/* Must be power of two */ #define ODEBUG_BATCH_SIZE 16
/* Initial values. Must all be a multiple of batch size */ #define ODEBUG_POOL_SIZE (64 * ODEBUG_BATCH_SIZE) #define ODEBUG_POOL_MIN_LEVEL (ODEBUG_POOL_SIZE / 4)
/* * We limit the freeing of debug objects via workqueue at a maximum * frequency of 10Hz and about 1024 objects for each freeing operation. * So it is freeing at most 10k debug objects per second.
*/ #define ODEBUG_FREE_WORK_MAX (1024 / ODEBUG_BATCH_SIZE) #define ODEBUG_FREE_WORK_DELAY DIV_ROUND_UP(HZ, 10)
/* Move the next batch to the front of the source pool */
src->objects.first = next_batch; if (next_batch)
next_batch->pprev = &src->objects.first;
/* Add the extracted batch to the destination pool */
last->next = dst->objects.first; if (last->next)
last->next->pprev = &last->next;
first_batch->pprev = &dst->objects.first;
dst->objects.first = first_batch;
/* Move the complete list to the head */
hlist_move_list(&src->objects, head);
obj = hlist_entry(head->first, typeof(*obj), node);
last = obj->batch_last;
next = last->next; /* Disconnect the batch from the list */
last->next = NULL;
/* Move the node after last back to the source pool. */
src->objects.first = next; if (next)
next->pprev = &src->objects.first;
for (;;) { struct debug_obj *obj = __alloc_object(&pcp->objects);
if (likely(obj)) {
pcp->cnt--; /* * If this emptied a batch try to refill from the * free pool. Don't do that if this was the top-most * batch as pcpu_free() expects the per CPU pool * to be less than ODEBUG_POOL_PERCPU_SIZE.
*/ if (unlikely(pcp->cnt < (ODEBUG_POOL_PERCPU_SIZE - ODEBUG_BATCH_SIZE) &&
!(pcp->cnt % ODEBUG_BATCH_SIZE))) { /* * Don't try to allocate from the regular pool here * to not exhaust it prematurely.
*/ if (pool_count(&pool_to_free)) {
guard(raw_spinlock)(&pool_lock);
pool_move_batch(pcp, &pool_to_free);
pcpu_refill_stats();
}
} return obj;
}
guard(raw_spinlock)(&pool_lock); if (!pool_move_batch(pcp, &pool_to_free)) { if (!pool_move_batch(pcp, &pool_global)) return NULL;
}
pcpu_refill_stats();
}
}
/* Pool full ? */ if (pcp->cnt < ODEBUG_POOL_PERCPU_SIZE) return;
/* Remove a batch from the per CPU pool */
guard(raw_spinlock)(&pool_lock); /* Try to fit the batch into the pool_global first */ if (!pool_move_batch(&pool_global, pcp))
pool_move_batch(&pool_to_free, pcp);
WRITE_ONCE(pool_global.stats.cur_used, pool_global.stats.cur_used - ODEBUG_BATCH_SIZE);
}
/* * Reuse objs from the global obj_to_free list; they will be * reinitialized when allocating.
*/ if (!pool_count(&pool_to_free)) return;
/* * Prevent the context from being scheduled or interrupted after * setting the state flag;
*/
guard(irqsave)();
/* * Avoid lock contention on &pool_lock and avoid making the cache * line exclusive by testing the bit before attempting to set it.
*/ if (test_bit(0, &state) || test_and_set_bit(0, &state)) return;
/* Avoid taking the lock when there is no work to do */ while (pool_should_refill(&pool_global) && pool_count(&pool_to_free)) {
guard(raw_spinlock)(&pool_lock); /* Move a batch if possible */
pool_move_batch(&pool_global, &pool_to_free);
}
clear_bit(0, &state);
}
/* * Avoid allocation and lock contention when: * - One other CPU is already allocating * - the global pool has not reached the critical level yet
*/ if (!pool_must_refill(&pool_global) && atomic_read(&cpus_allocating)) return;
atomic_inc(&cpus_allocating); while (pool_should_refill(&pool_global)) {
HLIST_HEAD(head);
if (!kmem_alloc_batch(&head, obj_cache, __GFP_HIGH | __GFP_NOWARN)) break;
guard(raw_spinlock_irqsave)(&pool_lock); if (!pool_push_batch(&pool_global, &head))
pool_push_batch(&pool_to_free, &head);
}
atomic_dec(&cpus_allocating);
}
/* * Lookup an object in the hash bucket.
*/ staticstruct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
{ struct debug_obj *obj; int cnt = 0;
hlist_for_each_entry(obj, &b->list, node) {
cnt++; if (obj->object == addr) return obj;
} if (cnt > debug_objects_maxchain)
debug_objects_maxchain = cnt;
/* Acquire and drop the lock for each batch */
scoped_guard(raw_spinlock_irqsave, &pool_lock) { if (!pool_to_free.cnt) return;
/* Refill the global pool if possible */ if (pool_move_batch(&pool_global, &pool_to_free)) { /* Don't free as there seems to be demand */
max_free = 0;
} elseif (max_free) {
pool_pop_batch(&tofree, &pool_to_free);
max_free--;
} else { return;
}
}
free_object_list(&tofree);
}
}
/* * Put the object back into the pool and schedule work to free objects * if necessary.
*/ staticvoid free_object(struct debug_obj *obj)
{
__free_object(obj); if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
WRITE_ONCE(obj_freeing, true);
schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
}
}
/* * Using free_object() puts the objects into reuse or schedules * them for freeing and it get's all the accounting correct.
*/
hlist_for_each_entry_safe(obj, tmp, list, node) {
hlist_del(&obj->node);
free_object(obj);
}
}
#ifdef CONFIG_HOTPLUG_CPU staticint object_cpu_offline(unsignedint cpu)
{ /* Remote access is safe as the CPU is dead already */ struct obj_pool *pcp = per_cpu_ptr(&pool_pcpu, cpu);
/* Out of memory. Free all objects from hash */ staticvoid debug_objects_oom(void)
{ struct debug_bucket *db = obj_hash;
HLIST_HEAD(freelist);
pr_warn("Out of memory. ODEBUG disabled\n");
for (int i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
scoped_guard(raw_spinlock_irqsave, &db->lock)
hlist_move_list(&db->list, &freelist);
put_objects(&freelist);
}
}
/* * We use the pfn of the address for the hash. That way we can check * for freed objects simply by checking the affected bucket.
*/ staticstruct debug_bucket *get_bucket(unsignedlong addr)
{ unsignedlong hash;
/* * Don't report if lookup_object_or_alloc() by the current thread * failed because lookup_object_or_alloc()/debug_objects_oom() by a * concurrent thread turned off debug_objects_enabled and cleared * the hash buckets.
*/ if (!debug_objects_enabled) return;
/* * Try to repair the damage, so we have a better chance to get useful * debug output.
*/ staticbool
debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state), void * addr, enum debug_obj_state state)
{ if (fixup && fixup(addr, state)) {
debug_objects_fixups++; returntrue;
} returnfalse;
}
staticvoid debug_object_is_on_stack(void *addr, int onstack)
{ int is_on_stack; staticint limit;
if (limit > 4) return;
is_on_stack = object_is_on_stack(addr); if (is_on_stack == onstack) return;
limit++; if (is_on_stack)
pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
task_stack_page(current)); else
pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
task_stack_page(current));
/* * debug_object_init() unconditionally allocates untracked * objects. It does not matter whether it is a static object or * not. * * debug_object_assert_init() and debug_object_activate() allow * allocation only if the descriptor callback confirms that the * object is static and considered initialized. For non-static * objects the allocation needs to be done from the fixup callback.
*/ if (unlikely(alloc_ifstatic)) { if (!descr->is_static_object || !descr->is_static_object(addr)) return ERR_PTR(-ENOENT); /* Statically allocated objects are considered initialized */
state = ODEBUG_STATE_INIT;
}
/* Out of memory. Do the cleanup outside of the locked region */
debug_objects_enabled = false; return NULL;
}
staticvoid debug_objects_fill_pool(void)
{ if (!static_branch_likely(&obj_cache_enabled)) return;
if (likely(!pool_should_refill(&pool_global))) return;
/* Try reusing objects from obj_to_free_list */
fill_pool_from_freelist();
if (likely(!pool_should_refill(&pool_global))) return;
/* * On RT enabled kernels the pool refill must happen in preemptible * context -- for !RT kernels we rely on the fact that spinlock_t and * raw_spinlock_t are basically the same type and this lock-type * inversion works just fine.
*/ if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible()) { /* * Annotate away the spinlock_t inside raw_spinlock_t warning * by temporarily raising the wait-type to WAIT_SLEEP, matching * the preemptible() condition above.
*/ static DEFINE_WAIT_OVERRIDE_MAP(fill_pool_map, LD_WAIT_SLEEP);
lock_map_acquire_try(&fill_pool_map);
fill_pool();
lock_map_release(&fill_pool_map);
}
}
switch (obj->state) { case ODEBUG_STATE_NONE: case ODEBUG_STATE_INIT: case ODEBUG_STATE_INACTIVE:
obj->state = ODEBUG_STATE_INIT;
raw_spin_unlock_irqrestore(&db->lock, flags); return; default: break;
}
o = *obj;
raw_spin_unlock_irqrestore(&db->lock, flags);
debug_print_object(&o, "init");
if (o.state == ODEBUG_STATE_ACTIVE)
debug_object_fixup(descr->fixup_init, addr, o.state);
}
/** * debug_object_init - debug checks when an object is initialized * @addr: address of the object * @descr: pointer to an object specific debug description structure
*/ void debug_object_init(void *addr, conststruct debug_obj_descr *descr)
{ if (!debug_objects_enabled) return;
/** * debug_object_init_on_stack - debug checks when an object on stack is * initialized * @addr: address of the object * @descr: pointer to an object specific debug description structure
*/ void debug_object_init_on_stack(void *addr, conststruct debug_obj_descr *descr)
{ if (!debug_objects_enabled) return;
switch (o.state) { case ODEBUG_STATE_ACTIVE: case ODEBUG_STATE_NOTAVAILABLE: if (debug_object_fixup(descr->fixup_activate, addr, o.state)) return 0;
fallthrough; default: return -EINVAL;
}
}
EXPORT_SYMBOL_GPL(debug_object_activate);
/** * debug_object_deactivate - debug checks when an object is deactivated * @addr: address of the object * @descr: pointer to an object specific debug description structure
*/ void debug_object_deactivate(void *addr, conststruct debug_obj_descr *descr)
{ struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr }; struct debug_bucket *db; struct debug_obj *obj; unsignedlong flags;
if (!debug_objects_enabled) return;
db = get_bucket((unsignedlong) addr);
raw_spin_lock_irqsave(&db->lock, flags);
obj = lookup_object(addr, db); if (obj) { switch (obj->state) { case ODEBUG_STATE_DESTROYED: break; case ODEBUG_STATE_INIT: case ODEBUG_STATE_INACTIVE: case ODEBUG_STATE_ACTIVE: if (obj->astate) break;
obj->state = ODEBUG_STATE_INACTIVE;
fallthrough; default:
raw_spin_unlock_irqrestore(&db->lock, flags); return;
}
o = *obj;
}
switch (obj->state) { case ODEBUG_STATE_ACTIVE: case ODEBUG_STATE_DESTROYED: break; case ODEBUG_STATE_NONE: case ODEBUG_STATE_INIT: case ODEBUG_STATE_INACTIVE:
obj->state = ODEBUG_STATE_DESTROYED;
fallthrough; default:
raw_spin_unlock_irqrestore(&db->lock, flags); return;
}
o = *obj;
raw_spin_unlock_irqrestore(&db->lock, flags);
debug_print_object(&o, "destroy");
if (o.state == ODEBUG_STATE_ACTIVE)
debug_object_fixup(descr->fixup_destroy, addr, o.state);
}
EXPORT_SYMBOL_GPL(debug_object_destroy);
/** * debug_object_free - debug checks when an object is freed * @addr: address of the object * @descr: pointer to an object specific debug description structure
*/ void debug_object_free(void *addr, conststruct debug_obj_descr *descr)
{ struct debug_obj *obj, o; struct debug_bucket *db; unsignedlong flags;
/* * pool_global.stats.cur_used is the number of batches currently * handed out to per CPU pools. Convert it to number of objects * and subtract the number of free objects in the per CPU pools. * As this is lockless the number is an estimate.
*/
for_each_possible_cpu(cpu)
pcp_free += per_cpu(pool_pcpu.cnt, cpu);
/* * fixup_activate is called when: * - an active object is activated * - an unknown non-static object is activated
*/ staticbool __init fixup_activate(void *addr, enum debug_obj_state state)
{ struct self_test *obj = addr;
switch (state) { case ODEBUG_STATE_NOTAVAILABLE: returntrue; case ODEBUG_STATE_ACTIVE:
debug_object_deactivate(obj, &descr_type_test);
debug_object_activate(obj, &descr_type_test); returntrue;
default: returnfalse;
}
}
/* * fixup_destroy is called when: * - an active object is destroyed
*/ staticbool __init fixup_destroy(void *addr, enum debug_obj_state state)
{ struct self_test *obj = addr;
/* * Called during early boot to initialize the hash buckets and link * the static object pool objects into the poll list. After this call * the object tracker is fully operational.
*/ void __init debug_objects_early_init(void)
{ int i;
for (i = 0; i < ODEBUG_HASH_SIZE; i++)
raw_spin_lock_init(&obj_hash[i].lock);
/* Keep early boot simple and add everything to the boot list */ for (i = 0; i < ODEBUG_POOL_SIZE; i++)
hlist_add_head(&obj_static_pool[i].node, &pool_boot);
}
/* * Convert the statically allocated objects to dynamic ones. * debug_objects_mem_init() is called early so only one CPU is up and * interrupts are disabled, which means it is safe to replace the active * object references.
*/ staticbool __init debug_objects_replace_static_objects(struct kmem_cache *cache)
{ struct debug_bucket *db = obj_hash; struct hlist_node *tmp; struct debug_obj *obj;
HLIST_HEAD(objects); int i;
for (i = 0; i < ODEBUG_POOL_SIZE; i += ODEBUG_BATCH_SIZE) { if (!kmem_alloc_batch(&objects, cache, GFP_KERNEL)) goto free;
pool_push_batch(&pool_global, &objects);
}
/* Disconnect the boot pool. */
pool_boot.first = NULL;
/* Replace the active object references */ for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
hlist_move_list(&db->list, &objects);
/* copy object data */
*new = *obj;
hlist_add_head(&new->node, &db->list);
}
} returntrue;
free: /* Can't use free_object_list() as the cache is not populated yet */
hlist_for_each_entry_safe(obj, tmp, &pool_global.objects, node) {
hlist_del(&obj->node);
kmem_cache_free(cache, obj);
} returnfalse;
}
/* * Called after the kmem_caches are functional to setup a dedicated * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag * prevents that the debug code is called on kmem_cache_free() for the * debug tracker objects to avoid recursive calls.
*/ void __init debug_objects_mem_init(void)
{ struct kmem_cache *cache; int extras;
if (!cache || !debug_objects_replace_static_objects(cache)) {
debug_objects_enabled = false;
pr_warn("Out of memory.\n"); return;
}
/* * Adjust the thresholds for allocating and freeing objects * according to the number of possible CPUs available in the * system.
*/
extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
pool_global.max_cnt += extras;
pool_global.min_cnt += extras;
¤ 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.0.12Bemerkung:
(vorverarbeitet)
¤
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 ist noch experimentell.