19e4a51adSThomas Gleixner // SPDX-License-Identifier: GPL-2.0 23ac7fe5aSThomas Gleixner /* 33ac7fe5aSThomas Gleixner * Generic infrastructure for lifetime debugging of objects. 43ac7fe5aSThomas Gleixner * 53ac7fe5aSThomas Gleixner * Copyright (C) 2008, Thomas Gleixner <[email protected]> 63ac7fe5aSThomas Gleixner */ 7719e4843SFabian Frederick 8719e4843SFabian Frederick #define pr_fmt(fmt) "ODEBUG: " fmt 9719e4843SFabian Frederick 1014077b9eSThomas Gleixner #include <linux/cpu.h> 113ac7fe5aSThomas Gleixner #include <linux/debugobjects.h> 1214077b9eSThomas Gleixner #include <linux/debugfs.h> 1314077b9eSThomas Gleixner #include <linux/hash.h> 1414077b9eSThomas Gleixner #include <linux/kmemleak.h> 15d43c36dcSAlexey Dobriyan #include <linux/sched.h> 1668db0cf1SIngo Molnar #include <linux/sched/task_stack.h> 173ac7fe5aSThomas Gleixner #include <linux/seq_file.h> 185a0e3ad6STejun Heo #include <linux/slab.h> 1914077b9eSThomas Gleixner #include <linux/static_key.h> 203ac7fe5aSThomas Gleixner 213ac7fe5aSThomas Gleixner #define ODEBUG_HASH_BITS 14 223ac7fe5aSThomas Gleixner #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS) 233ac7fe5aSThomas Gleixner 2474fe1ad4SThomas Gleixner /* Must be power of two */ 25634d61f4SWaiman Long #define ODEBUG_BATCH_SIZE 16 263ac7fe5aSThomas Gleixner 2774fe1ad4SThomas Gleixner /* Initial values. Must all be a multiple of batch size */ 2874fe1ad4SThomas Gleixner #define ODEBUG_POOL_SIZE (64 * ODEBUG_BATCH_SIZE) 2974fe1ad4SThomas Gleixner #define ODEBUG_POOL_MIN_LEVEL (ODEBUG_POOL_SIZE / 4) 3074fe1ad4SThomas Gleixner 31*a201a96bSThomas Gleixner #define ODEBUG_POOL_PERCPU_SIZE (8 * ODEBUG_BATCH_SIZE) 3274fe1ad4SThomas Gleixner 333ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT 343ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT) 353ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1)) 363ac7fe5aSThomas Gleixner 37a7344a68SWaiman Long /* 38a7344a68SWaiman Long * We limit the freeing of debug objects via workqueue at a maximum 39a7344a68SWaiman Long * frequency of 10Hz and about 1024 objects for each freeing operation. 40a7344a68SWaiman Long * So it is freeing at most 10k debug objects per second. 41a7344a68SWaiman Long */ 429ce99c6dSThomas Gleixner #define ODEBUG_FREE_WORK_MAX (1024 / ODEBUG_BATCH_SIZE) 43a7344a68SWaiman Long #define ODEBUG_FREE_WORK_DELAY DIV_ROUND_UP(HZ, 10) 44a7344a68SWaiman Long 453ac7fe5aSThomas Gleixner struct debug_bucket { 463ac7fe5aSThomas Gleixner struct hlist_head list; 47aef9cb05SThomas Gleixner raw_spinlock_t lock; 483ac7fe5aSThomas Gleixner }; 493ac7fe5aSThomas Gleixner 502638345dSThomas Gleixner struct pool_stats { 512638345dSThomas Gleixner unsigned int cur_used; 522638345dSThomas Gleixner unsigned int max_used; 532638345dSThomas Gleixner unsigned int min_fill; 542638345dSThomas Gleixner }; 552638345dSThomas Gleixner 56e18328ffSThomas Gleixner struct obj_pool { 57e18328ffSThomas Gleixner struct hlist_head objects; 58e18328ffSThomas Gleixner unsigned int cnt; 5996a9a042SThomas Gleixner unsigned int min_cnt; 6096a9a042SThomas Gleixner unsigned int max_cnt; 612638345dSThomas Gleixner struct pool_stats stats; 62e18328ffSThomas Gleixner } ____cacheline_aligned; 63e18328ffSThomas Gleixner 6496a9a042SThomas Gleixner 6596a9a042SThomas Gleixner static DEFINE_PER_CPU_ALIGNED(struct obj_pool, pool_pcpu) = { 6696a9a042SThomas Gleixner .max_cnt = ODEBUG_POOL_PERCPU_SIZE, 6796a9a042SThomas Gleixner }; 68d86998b1SWaiman Long 693ac7fe5aSThomas Gleixner static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE]; 703ac7fe5aSThomas Gleixner 711be1cb7bSThomas Gleixner static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata; 723ac7fe5aSThomas Gleixner 73aef9cb05SThomas Gleixner static DEFINE_RAW_SPINLOCK(pool_lock); 743ac7fe5aSThomas Gleixner 7596a9a042SThomas Gleixner static struct obj_pool pool_global = { 7696a9a042SThomas Gleixner .min_cnt = ODEBUG_POOL_MIN_LEVEL, 7796a9a042SThomas Gleixner .max_cnt = ODEBUG_POOL_SIZE, 782638345dSThomas Gleixner .stats = { 792638345dSThomas Gleixner .min_fill = ODEBUG_POOL_SIZE, 802638345dSThomas Gleixner }, 8196a9a042SThomas Gleixner }; 8296a9a042SThomas Gleixner 8396a9a042SThomas Gleixner static struct obj_pool pool_to_free = { 8496a9a042SThomas Gleixner .max_cnt = UINT_MAX, 8596a9a042SThomas Gleixner }; 863ac7fe5aSThomas Gleixner 87cb58d190SThomas Gleixner static HLIST_HEAD(pool_boot); 88cb58d190SThomas Gleixner 89a7344a68SWaiman Long static bool obj_freeing; 903ac7fe5aSThomas Gleixner 915b5baba6SBreno Leitao static int __data_racy debug_objects_maxchain __read_mostly; 925b5baba6SBreno Leitao static int __data_racy __maybe_unused debug_objects_maxchecked __read_mostly; 935b5baba6SBreno Leitao static int __data_racy debug_objects_fixups __read_mostly; 945b5baba6SBreno Leitao static int __data_racy debug_objects_warnings __read_mostly; 95661cc28bSThomas Gleixner static bool __data_racy debug_objects_enabled __read_mostly 963ae70205SIngo Molnar = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT; 975b5baba6SBreno Leitao 98aedcade6SStephen Boyd static const struct debug_obj_descr *descr_test __read_mostly; 9968279f9cSAlexey Dobriyan static struct kmem_cache *obj_cache __ro_after_init; 1003ac7fe5aSThomas Gleixner 101c4b73aabSWaiman Long /* 1020cad93c3SWaiman Long * Track numbers of kmem_cache_alloc()/free() calls done. 103c4b73aabSWaiman Long */ 104e4757c71SZhen Lei static int __data_racy debug_objects_allocated; 105e4757c71SZhen Lei static int __data_racy debug_objects_freed; 106c4b73aabSWaiman Long 107337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work); 108a7344a68SWaiman Long static DECLARE_DELAYED_WORK(debug_obj_work, free_obj_work); 109337fff8bSThomas Gleixner 11014077b9eSThomas Gleixner static DEFINE_STATIC_KEY_FALSE(obj_cache_enabled); 11114077b9eSThomas Gleixner 1123ac7fe5aSThomas Gleixner static int __init enable_object_debug(char *str) 1133ac7fe5aSThomas Gleixner { 114661cc28bSThomas Gleixner debug_objects_enabled = true; 1153ac7fe5aSThomas Gleixner return 0; 1163ac7fe5aSThomas Gleixner } 117661cc28bSThomas Gleixner early_param("debug_objects", enable_object_debug); 1183e8ebb5cSKyle McMartin 1193e8ebb5cSKyle McMartin static int __init disable_object_debug(char *str) 1203e8ebb5cSKyle McMartin { 121661cc28bSThomas Gleixner debug_objects_enabled = false; 1223e8ebb5cSKyle McMartin return 0; 1233e8ebb5cSKyle McMartin } 1243e8ebb5cSKyle McMartin early_param("no_debug_objects", disable_object_debug); 1253ac7fe5aSThomas Gleixner 1263ac7fe5aSThomas Gleixner static const char *obj_states[ODEBUG_STATE_MAX] = { 1273ac7fe5aSThomas Gleixner [ODEBUG_STATE_NONE] = "none", 1283ac7fe5aSThomas Gleixner [ODEBUG_STATE_INIT] = "initialized", 1293ac7fe5aSThomas Gleixner [ODEBUG_STATE_INACTIVE] = "inactive", 1303ac7fe5aSThomas Gleixner [ODEBUG_STATE_ACTIVE] = "active", 1313ac7fe5aSThomas Gleixner [ODEBUG_STATE_DESTROYED] = "destroyed", 1323ac7fe5aSThomas Gleixner [ODEBUG_STATE_NOTAVAILABLE] = "not available", 1333ac7fe5aSThomas Gleixner }; 1343ac7fe5aSThomas Gleixner 135e18328ffSThomas Gleixner static __always_inline unsigned int pool_count(struct obj_pool *pool) 136e18328ffSThomas Gleixner { 137e18328ffSThomas Gleixner return READ_ONCE(pool->cnt); 138e18328ffSThomas Gleixner } 139e18328ffSThomas Gleixner 14096a9a042SThomas Gleixner static __always_inline bool pool_should_refill(struct obj_pool *pool) 141e18328ffSThomas Gleixner { 14296a9a042SThomas Gleixner return pool_count(pool) < pool->min_cnt; 143e18328ffSThomas Gleixner } 144e18328ffSThomas Gleixner 14596a9a042SThomas Gleixner static __always_inline bool pool_must_refill(struct obj_pool *pool) 146e18328ffSThomas Gleixner { 14796a9a042SThomas Gleixner return pool_count(pool) < pool->min_cnt / 2; 148e18328ffSThomas Gleixner } 149e18328ffSThomas Gleixner 150fb60c004SThomas Gleixner static bool pool_move_batch(struct obj_pool *dst, struct obj_pool *src) 151fb60c004SThomas Gleixner { 152f57ebb92SThomas Gleixner struct hlist_node *last, *next_batch, *first_batch; 153f57ebb92SThomas Gleixner struct debug_obj *obj; 154f57ebb92SThomas Gleixner 155f57ebb92SThomas Gleixner if (dst->cnt >= dst->max_cnt || !src->cnt) 156fb60c004SThomas Gleixner return false; 157fb60c004SThomas Gleixner 158f57ebb92SThomas Gleixner first_batch = src->objects.first; 159f57ebb92SThomas Gleixner obj = hlist_entry(first_batch, typeof(*obj), node); 160f57ebb92SThomas Gleixner last = obj->batch_last; 161f57ebb92SThomas Gleixner next_batch = last->next; 162fb60c004SThomas Gleixner 163f57ebb92SThomas Gleixner /* Move the next batch to the front of the source pool */ 164f57ebb92SThomas Gleixner src->objects.first = next_batch; 165f57ebb92SThomas Gleixner if (next_batch) 166f57ebb92SThomas Gleixner next_batch->pprev = &src->objects.first; 167fb60c004SThomas Gleixner 168f57ebb92SThomas Gleixner /* Add the extracted batch to the destination pool */ 169f57ebb92SThomas Gleixner last->next = dst->objects.first; 170f57ebb92SThomas Gleixner if (last->next) 171f57ebb92SThomas Gleixner last->next->pprev = &last->next; 172f57ebb92SThomas Gleixner first_batch->pprev = &dst->objects.first; 173f57ebb92SThomas Gleixner dst->objects.first = first_batch; 174f57ebb92SThomas Gleixner 175f57ebb92SThomas Gleixner WRITE_ONCE(src->cnt, src->cnt - ODEBUG_BATCH_SIZE); 176f57ebb92SThomas Gleixner WRITE_ONCE(dst->cnt, dst->cnt + ODEBUG_BATCH_SIZE); 177fb60c004SThomas Gleixner return true; 178fb60c004SThomas Gleixner } 179fb60c004SThomas Gleixner 180aebbfe07SThomas Gleixner static bool pool_push_batch(struct obj_pool *dst, struct hlist_head *head) 181aebbfe07SThomas Gleixner { 182aebbfe07SThomas Gleixner struct hlist_node *last; 183aebbfe07SThomas Gleixner struct debug_obj *obj; 184aebbfe07SThomas Gleixner 185aebbfe07SThomas Gleixner if (dst->cnt >= dst->max_cnt) 186aebbfe07SThomas Gleixner return false; 187aebbfe07SThomas Gleixner 188aebbfe07SThomas Gleixner obj = hlist_entry(head->first, typeof(*obj), node); 189aebbfe07SThomas Gleixner last = obj->batch_last; 190aebbfe07SThomas Gleixner 191aebbfe07SThomas Gleixner hlist_splice_init(head, last, &dst->objects); 192aebbfe07SThomas Gleixner WRITE_ONCE(dst->cnt, dst->cnt + ODEBUG_BATCH_SIZE); 193aebbfe07SThomas Gleixner return true; 194aebbfe07SThomas Gleixner } 195aebbfe07SThomas Gleixner 1969ce99c6dSThomas Gleixner static bool pool_pop_batch(struct hlist_head *head, struct obj_pool *src) 1979ce99c6dSThomas Gleixner { 198f57ebb92SThomas Gleixner struct hlist_node *last, *next; 199f57ebb92SThomas Gleixner struct debug_obj *obj; 200f57ebb92SThomas Gleixner 2019ce99c6dSThomas Gleixner if (!src->cnt) 2029ce99c6dSThomas Gleixner return false; 2039ce99c6dSThomas Gleixner 204f57ebb92SThomas Gleixner /* Move the complete list to the head */ 205f57ebb92SThomas Gleixner hlist_move_list(&src->objects, head); 2069ce99c6dSThomas Gleixner 207f57ebb92SThomas Gleixner obj = hlist_entry(head->first, typeof(*obj), node); 208f57ebb92SThomas Gleixner last = obj->batch_last; 209f57ebb92SThomas Gleixner next = last->next; 210f57ebb92SThomas Gleixner /* Disconnect the batch from the list */ 211f57ebb92SThomas Gleixner last->next = NULL; 212f57ebb92SThomas Gleixner 213f57ebb92SThomas Gleixner /* Move the node after last back to the source pool. */ 214f57ebb92SThomas Gleixner src->objects.first = next; 215f57ebb92SThomas Gleixner if (next) 216f57ebb92SThomas Gleixner next->pprev = &src->objects.first; 217f57ebb92SThomas Gleixner 218f57ebb92SThomas Gleixner WRITE_ONCE(src->cnt, src->cnt - ODEBUG_BATCH_SIZE); 2199ce99c6dSThomas Gleixner return true; 2209ce99c6dSThomas Gleixner } 2219ce99c6dSThomas Gleixner 222fb60c004SThomas Gleixner static struct debug_obj *__alloc_object(struct hlist_head *list) 223fb60c004SThomas Gleixner { 224fb60c004SThomas Gleixner struct debug_obj *obj; 225fb60c004SThomas Gleixner 226fb60c004SThomas Gleixner if (unlikely(!list->first)) 227fb60c004SThomas Gleixner return NULL; 228fb60c004SThomas Gleixner 229fb60c004SThomas Gleixner obj = hlist_entry(list->first, typeof(*obj), node); 230fb60c004SThomas Gleixner hlist_del(&obj->node); 231fb60c004SThomas Gleixner return obj; 232fb60c004SThomas Gleixner } 233fb60c004SThomas Gleixner 2342638345dSThomas Gleixner static void pcpu_refill_stats(void) 2352638345dSThomas Gleixner { 2362638345dSThomas Gleixner struct pool_stats *stats = &pool_global.stats; 2372638345dSThomas Gleixner 2382638345dSThomas Gleixner WRITE_ONCE(stats->cur_used, stats->cur_used + ODEBUG_BATCH_SIZE); 2392638345dSThomas Gleixner 2402638345dSThomas Gleixner if (stats->cur_used > stats->max_used) 2412638345dSThomas Gleixner stats->max_used = stats->cur_used; 2422638345dSThomas Gleixner 2432638345dSThomas Gleixner if (pool_global.cnt < stats->min_fill) 2442638345dSThomas Gleixner stats->min_fill = pool_global.cnt; 2452638345dSThomas Gleixner } 2462638345dSThomas Gleixner 247fb60c004SThomas Gleixner static struct debug_obj *pcpu_alloc(void) 248fb60c004SThomas Gleixner { 249fb60c004SThomas Gleixner struct obj_pool *pcp = this_cpu_ptr(&pool_pcpu); 250fb60c004SThomas Gleixner 251fb60c004SThomas Gleixner lockdep_assert_irqs_disabled(); 252fb60c004SThomas Gleixner 253fb60c004SThomas Gleixner for (;;) { 254fb60c004SThomas Gleixner struct debug_obj *obj = __alloc_object(&pcp->objects); 255fb60c004SThomas Gleixner 256fb60c004SThomas Gleixner if (likely(obj)) { 257fb60c004SThomas Gleixner pcp->cnt--; 258fb60c004SThomas Gleixner return obj; 259fb60c004SThomas Gleixner } 260fb60c004SThomas Gleixner 261fb60c004SThomas Gleixner guard(raw_spinlock)(&pool_lock); 262fb60c004SThomas Gleixner if (!pool_move_batch(pcp, &pool_to_free)) { 263fb60c004SThomas Gleixner if (!pool_move_batch(pcp, &pool_global)) 264fb60c004SThomas Gleixner return NULL; 265fb60c004SThomas Gleixner } 2662638345dSThomas Gleixner pcpu_refill_stats(); 267fb60c004SThomas Gleixner } 268fb60c004SThomas Gleixner } 269fb60c004SThomas Gleixner 270a3b9e191SThomas Gleixner static void pcpu_free(struct debug_obj *obj) 271a3b9e191SThomas Gleixner { 272a3b9e191SThomas Gleixner struct obj_pool *pcp = this_cpu_ptr(&pool_pcpu); 273f57ebb92SThomas Gleixner struct debug_obj *first; 274a3b9e191SThomas Gleixner 275a3b9e191SThomas Gleixner lockdep_assert_irqs_disabled(); 276a3b9e191SThomas Gleixner 277f57ebb92SThomas Gleixner if (!(pcp->cnt % ODEBUG_BATCH_SIZE)) { 278f57ebb92SThomas Gleixner obj->batch_last = &obj->node; 279f57ebb92SThomas Gleixner } else { 280f57ebb92SThomas Gleixner first = hlist_entry(pcp->objects.first, typeof(*first), node); 281f57ebb92SThomas Gleixner obj->batch_last = first->batch_last; 282f57ebb92SThomas Gleixner } 283a3b9e191SThomas Gleixner hlist_add_head(&obj->node, &pcp->objects); 284a3b9e191SThomas Gleixner pcp->cnt++; 285a3b9e191SThomas Gleixner 286a3b9e191SThomas Gleixner /* Pool full ? */ 287a3b9e191SThomas Gleixner if (pcp->cnt < ODEBUG_POOL_PERCPU_SIZE) 288a3b9e191SThomas Gleixner return; 289a3b9e191SThomas Gleixner 290a3b9e191SThomas Gleixner /* Remove a batch from the per CPU pool */ 291a3b9e191SThomas Gleixner guard(raw_spinlock)(&pool_lock); 292a3b9e191SThomas Gleixner /* Try to fit the batch into the pool_global first */ 293a3b9e191SThomas Gleixner if (!pool_move_batch(&pool_global, pcp)) 294a3b9e191SThomas Gleixner pool_move_batch(&pool_to_free, pcp); 2952638345dSThomas Gleixner WRITE_ONCE(pool_global.stats.cur_used, pool_global.stats.cur_used - ODEBUG_BATCH_SIZE); 296a3b9e191SThomas Gleixner } 297a3b9e191SThomas Gleixner 29849a5cb82SThomas Gleixner static void free_object_list(struct hlist_head *head) 29949a5cb82SThomas Gleixner { 30049a5cb82SThomas Gleixner struct hlist_node *tmp; 30149a5cb82SThomas Gleixner struct debug_obj *obj; 30249a5cb82SThomas Gleixner int cnt = 0; 30349a5cb82SThomas Gleixner 30449a5cb82SThomas Gleixner hlist_for_each_entry_safe(obj, tmp, head, node) { 30549a5cb82SThomas Gleixner hlist_del(&obj->node); 30649a5cb82SThomas Gleixner kmem_cache_free(obj_cache, obj); 30749a5cb82SThomas Gleixner cnt++; 30849a5cb82SThomas Gleixner } 30949a5cb82SThomas Gleixner debug_objects_freed += cnt; 31049a5cb82SThomas Gleixner } 31149a5cb82SThomas Gleixner 312d8c6cd3aSZhen Lei static void fill_pool_from_freelist(void) 3133ac7fe5aSThomas Gleixner { 314d8c6cd3aSZhen Lei static unsigned long state; 3153ac7fe5aSThomas Gleixner 31636c4ead6SYang Shi /* 31763a4a9b5SZhen Lei * Reuse objs from the global obj_to_free list; they will be 31863a4a9b5SZhen Lei * reinitialized when allocating. 31936c4ead6SYang Shi */ 320e18328ffSThomas Gleixner if (!pool_count(&pool_to_free)) 321d8c6cd3aSZhen Lei return; 322d8c6cd3aSZhen Lei 323d8c6cd3aSZhen Lei /* 324d8c6cd3aSZhen Lei * Prevent the context from being scheduled or interrupted after 325d8c6cd3aSZhen Lei * setting the state flag; 326d8c6cd3aSZhen Lei */ 327d8c6cd3aSZhen Lei guard(irqsave)(); 328d8c6cd3aSZhen Lei 329d8c6cd3aSZhen Lei /* 330d8c6cd3aSZhen Lei * Avoid lock contention on &pool_lock and avoid making the cache 331d8c6cd3aSZhen Lei * line exclusive by testing the bit before attempting to set it. 332d8c6cd3aSZhen Lei */ 333d8c6cd3aSZhen Lei if (test_bit(0, &state) || test_and_set_bit(0, &state)) 334d8c6cd3aSZhen Lei return; 335d8c6cd3aSZhen Lei 336fb60c004SThomas Gleixner /* Avoid taking the lock when there is no work to do */ 337fb60c004SThomas Gleixner while (pool_should_refill(&pool_global) && pool_count(&pool_to_free)) { 338d8c6cd3aSZhen Lei guard(raw_spinlock)(&pool_lock); 339fb60c004SThomas Gleixner /* Move a batch if possible */ 340fb60c004SThomas Gleixner pool_move_batch(&pool_global, &pool_to_free); 34136c4ead6SYang Shi } 342d8c6cd3aSZhen Lei clear_bit(0, &state); 34336c4ead6SYang Shi } 34436c4ead6SYang Shi 345aebbfe07SThomas Gleixner static bool kmem_alloc_batch(struct hlist_head *head, struct kmem_cache *cache, gfp_t gfp) 346aebbfe07SThomas Gleixner { 347aebbfe07SThomas Gleixner struct hlist_node *last = NULL; 348aebbfe07SThomas Gleixner struct debug_obj *obj; 349aebbfe07SThomas Gleixner 350aebbfe07SThomas Gleixner for (int cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) { 351aebbfe07SThomas Gleixner obj = kmem_cache_zalloc(cache, gfp); 352aebbfe07SThomas Gleixner if (!obj) { 353aebbfe07SThomas Gleixner free_object_list(head); 354aebbfe07SThomas Gleixner return false; 355aebbfe07SThomas Gleixner } 356aebbfe07SThomas Gleixner debug_objects_allocated++; 357aebbfe07SThomas Gleixner 358aebbfe07SThomas Gleixner if (!last) 359aebbfe07SThomas Gleixner last = &obj->node; 360aebbfe07SThomas Gleixner obj->batch_last = last; 361aebbfe07SThomas Gleixner 362aebbfe07SThomas Gleixner hlist_add_head(&obj->node, head); 363aebbfe07SThomas Gleixner } 364aebbfe07SThomas Gleixner return true; 365aebbfe07SThomas Gleixner } 366aebbfe07SThomas Gleixner 367d8c6cd3aSZhen Lei static void fill_pool(void) 368d8c6cd3aSZhen Lei { 369d8c6cd3aSZhen Lei static atomic_t cpus_allocating; 370d8c6cd3aSZhen Lei 371d8c6cd3aSZhen Lei /* 372d8c6cd3aSZhen Lei * Avoid allocation and lock contention when: 373d8c6cd3aSZhen Lei * - One other CPU is already allocating 374d8c6cd3aSZhen Lei * - the global pool has not reached the critical level yet 375d8c6cd3aSZhen Lei */ 37696a9a042SThomas Gleixner if (!pool_must_refill(&pool_global) && atomic_read(&cpus_allocating)) 3771fda107dSThomas Gleixner return; 3783ac7fe5aSThomas Gleixner 379d8c6cd3aSZhen Lei atomic_inc(&cpus_allocating); 38096a9a042SThomas Gleixner while (pool_should_refill(&pool_global)) { 381813fd078SZhen Lei HLIST_HEAD(head); 3823ac7fe5aSThomas Gleixner 383aebbfe07SThomas Gleixner if (!kmem_alloc_batch(&head, obj_cache, __GFP_HIGH | __GFP_NOWARN)) 384d8c6cd3aSZhen Lei break; 3853ac7fe5aSThomas Gleixner 386d8c6cd3aSZhen Lei guard(raw_spinlock_irqsave)(&pool_lock); 387aebbfe07SThomas Gleixner if (!pool_push_batch(&pool_global, &head)) 388aebbfe07SThomas Gleixner pool_push_batch(&pool_to_free, &head); 3893ac7fe5aSThomas Gleixner } 390d8c6cd3aSZhen Lei atomic_dec(&cpus_allocating); 3913ac7fe5aSThomas Gleixner } 3923ac7fe5aSThomas Gleixner 3933ac7fe5aSThomas Gleixner /* 3943ac7fe5aSThomas Gleixner * Lookup an object in the hash bucket. 3953ac7fe5aSThomas Gleixner */ 3963ac7fe5aSThomas Gleixner static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b) 3973ac7fe5aSThomas Gleixner { 3983ac7fe5aSThomas Gleixner struct debug_obj *obj; 3993ac7fe5aSThomas Gleixner int cnt = 0; 4003ac7fe5aSThomas Gleixner 401b67bfe0dSSasha Levin hlist_for_each_entry(obj, &b->list, node) { 4023ac7fe5aSThomas Gleixner cnt++; 4033ac7fe5aSThomas Gleixner if (obj->object == addr) 4043ac7fe5aSThomas Gleixner return obj; 4053ac7fe5aSThomas Gleixner } 4063ac7fe5aSThomas Gleixner if (cnt > debug_objects_maxchain) 4073ac7fe5aSThomas Gleixner debug_objects_maxchain = cnt; 4083ac7fe5aSThomas Gleixner 4093ac7fe5aSThomas Gleixner return NULL; 4103ac7fe5aSThomas Gleixner } 4113ac7fe5aSThomas Gleixner 412fb60c004SThomas Gleixner static struct debug_obj *alloc_object(void *addr, struct debug_bucket *b, 413fb60c004SThomas Gleixner const struct debug_obj_descr *descr) 414d86998b1SWaiman Long { 415d86998b1SWaiman Long struct debug_obj *obj; 416d86998b1SWaiman Long 41714077b9eSThomas Gleixner if (static_branch_likely(&obj_cache_enabled)) 418fb60c004SThomas Gleixner obj = pcpu_alloc(); 419fb60c004SThomas Gleixner else 420cb58d190SThomas Gleixner obj = __alloc_object(&pool_boot); 4213ac7fe5aSThomas Gleixner 422fb60c004SThomas Gleixner if (likely(obj)) { 423d86998b1SWaiman Long obj->object = addr; 424d86998b1SWaiman Long obj->descr = descr; 425d86998b1SWaiman Long obj->state = ODEBUG_STATE_NONE; 426d86998b1SWaiman Long obj->astate = 0; 427d86998b1SWaiman Long hlist_add_head(&obj->node, &b->list); 428d86998b1SWaiman Long } 4293ac7fe5aSThomas Gleixner return obj; 4303ac7fe5aSThomas Gleixner } 4313ac7fe5aSThomas Gleixner 4329ce99c6dSThomas Gleixner /* workqueue function to free objects. */ 433337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work) 434337fff8bSThomas Gleixner { 4359ce99c6dSThomas Gleixner bool free = true; 436337fff8bSThomas Gleixner 437a7344a68SWaiman Long WRITE_ONCE(obj_freeing, false); 4389ce99c6dSThomas Gleixner 4399ce99c6dSThomas Gleixner if (!pool_count(&pool_to_free)) 440858274b6SWaiman Long return; 44136c4ead6SYang Shi 4429ce99c6dSThomas Gleixner for (unsigned int cnt = 0; cnt < ODEBUG_FREE_WORK_MAX; cnt++) { 4439ce99c6dSThomas Gleixner HLIST_HEAD(tofree); 444a7344a68SWaiman Long 4459ce99c6dSThomas Gleixner /* Acquire and drop the lock for each batch */ 4469ce99c6dSThomas Gleixner scoped_guard(raw_spinlock_irqsave, &pool_lock) { 4479ce99c6dSThomas Gleixner if (!pool_to_free.cnt) 448a7344a68SWaiman Long return; 44936c4ead6SYang Shi 4509ce99c6dSThomas Gleixner /* Refill the global pool if possible */ 4519ce99c6dSThomas Gleixner if (pool_move_batch(&pool_global, &pool_to_free)) { 4529ce99c6dSThomas Gleixner /* Don't free as there seems to be demand */ 4539ce99c6dSThomas Gleixner free = false; 4549ce99c6dSThomas Gleixner } else if (free) { 4559ce99c6dSThomas Gleixner pool_pop_batch(&tofree, &pool_to_free); 4569ce99c6dSThomas Gleixner } else { 4579ce99c6dSThomas Gleixner return; 45836c4ead6SYang Shi } 4599ce99c6dSThomas Gleixner } 46049a5cb82SThomas Gleixner free_object_list(&tofree); 461337fff8bSThomas Gleixner } 4629ce99c6dSThomas Gleixner } 463337fff8bSThomas Gleixner 464a7344a68SWaiman Long static void __free_object(struct debug_obj *obj) 465636e1970SYang Shi { 466cb58d190SThomas Gleixner guard(irqsave)(); 46714077b9eSThomas Gleixner if (static_branch_likely(&obj_cache_enabled)) 468a3b9e191SThomas Gleixner pcpu_free(obj); 469a3b9e191SThomas Gleixner else 470cb58d190SThomas Gleixner hlist_add_head(&obj->node, &pool_boot); 471636e1970SYang Shi } 472636e1970SYang Shi 473337fff8bSThomas Gleixner /* 474337fff8bSThomas Gleixner * Put the object back into the pool and schedule work to free objects 475337fff8bSThomas Gleixner * if necessary. 4763ac7fe5aSThomas Gleixner */ 4773ac7fe5aSThomas Gleixner static void free_object(struct debug_obj *obj) 4783ac7fe5aSThomas Gleixner { 479a7344a68SWaiman Long __free_object(obj); 480e18328ffSThomas Gleixner if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) { 481a7344a68SWaiman Long WRITE_ONCE(obj_freeing, true); 482a7344a68SWaiman Long schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY); 483a7344a68SWaiman Long } 4843ac7fe5aSThomas Gleixner } 4853ac7fe5aSThomas Gleixner 486a2a70238SThomas Gleixner static void put_objects(struct hlist_head *list) 48788451f2cSZqiang { 48888451f2cSZqiang struct hlist_node *tmp; 48988451f2cSZqiang struct debug_obj *obj; 49088451f2cSZqiang 491a2a70238SThomas Gleixner /* 492a2a70238SThomas Gleixner * Using free_object() puts the objects into reuse or schedules 493a2a70238SThomas Gleixner * them for freeing and it get's all the accounting correct. 494a2a70238SThomas Gleixner */ 495a2a70238SThomas Gleixner hlist_for_each_entry_safe(obj, tmp, list, node) { 49688451f2cSZqiang hlist_del(&obj->node); 497a2a70238SThomas Gleixner free_object(obj); 498a2a70238SThomas Gleixner } 49988451f2cSZqiang } 500eabb7f1aSwuchi 50149968cf1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU 502a2a70238SThomas Gleixner static int object_cpu_offline(unsigned int cpu) 503a2a70238SThomas Gleixner { 504a2a70238SThomas Gleixner /* Remote access is safe as the CPU is dead already */ 50518b8afcbSThomas Gleixner struct obj_pool *pcp = per_cpu_ptr(&pool_pcpu, cpu); 506eabb7f1aSwuchi 50718b8afcbSThomas Gleixner put_objects(&pcp->objects); 50818b8afcbSThomas Gleixner pcp->cnt = 0; 50988451f2cSZqiang return 0; 51088451f2cSZqiang } 51188451f2cSZqiang #endif 51288451f2cSZqiang 51349968cf1SThomas Gleixner /* Out of memory. Free all objects from hash */ 5143ac7fe5aSThomas Gleixner static void debug_objects_oom(void) 5153ac7fe5aSThomas Gleixner { 5163ac7fe5aSThomas Gleixner struct debug_bucket *db = obj_hash; 517673d62ccSVegard Nossum HLIST_HEAD(freelist); 5183ac7fe5aSThomas Gleixner 519719e4843SFabian Frederick pr_warn("Out of memory. ODEBUG disabled\n"); 5203ac7fe5aSThomas Gleixner 52149968cf1SThomas Gleixner for (int i = 0; i < ODEBUG_HASH_SIZE; i++, db++) { 52249968cf1SThomas Gleixner scoped_guard(raw_spinlock_irqsave, &db->lock) 523673d62ccSVegard Nossum hlist_move_list(&db->list, &freelist); 524673d62ccSVegard Nossum 52549968cf1SThomas Gleixner put_objects(&freelist); 5263ac7fe5aSThomas Gleixner } 5273ac7fe5aSThomas Gleixner } 5283ac7fe5aSThomas Gleixner 5293ac7fe5aSThomas Gleixner /* 5303ac7fe5aSThomas Gleixner * We use the pfn of the address for the hash. That way we can check 5313ac7fe5aSThomas Gleixner * for freed objects simply by checking the affected bucket. 5323ac7fe5aSThomas Gleixner */ 5333ac7fe5aSThomas Gleixner static struct debug_bucket *get_bucket(unsigned long addr) 5343ac7fe5aSThomas Gleixner { 5353ac7fe5aSThomas Gleixner unsigned long hash; 5363ac7fe5aSThomas Gleixner 5373ac7fe5aSThomas Gleixner hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS); 5383ac7fe5aSThomas Gleixner return &obj_hash[hash]; 5393ac7fe5aSThomas Gleixner } 5403ac7fe5aSThomas Gleixner 5413ac7fe5aSThomas Gleixner static void debug_print_object(struct debug_obj *obj, char *msg) 5423ac7fe5aSThomas Gleixner { 543aedcade6SStephen Boyd const struct debug_obj_descr *descr = obj->descr; 5443ac7fe5aSThomas Gleixner static int limit; 5453ac7fe5aSThomas Gleixner 5468b64d420STetsuo Handa /* 5478b64d420STetsuo Handa * Don't report if lookup_object_or_alloc() by the current thread 5488b64d420STetsuo Handa * failed because lookup_object_or_alloc()/debug_objects_oom() by a 5498b64d420STetsuo Handa * concurrent thread turned off debug_objects_enabled and cleared 5508b64d420STetsuo Handa * the hash buckets. 5518b64d420STetsuo Handa */ 5528b64d420STetsuo Handa if (!debug_objects_enabled) 5538b64d420STetsuo Handa return; 5548b64d420STetsuo Handa 55599777288SStanislaw Gruszka if (limit < 5 && descr != descr_test) { 55699777288SStanislaw Gruszka void *hint = descr->debug_hint ? 55799777288SStanislaw Gruszka descr->debug_hint(obj->object) : NULL; 5583ac7fe5aSThomas Gleixner limit++; 559a5d8e467SMathieu Desnoyers WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) " 560c4db2d3bSStephen Boyd "object: %p object type: %s hint: %pS\n", 561a5d8e467SMathieu Desnoyers msg, obj_states[obj->state], obj->astate, 562c4db2d3bSStephen Boyd obj->object, descr->name, hint); 5633ac7fe5aSThomas Gleixner } 5643ac7fe5aSThomas Gleixner debug_objects_warnings++; 5653ac7fe5aSThomas Gleixner } 5663ac7fe5aSThomas Gleixner 5673ac7fe5aSThomas Gleixner /* 5683ac7fe5aSThomas Gleixner * Try to repair the damage, so we have a better chance to get useful 5693ac7fe5aSThomas Gleixner * debug output. 5703ac7fe5aSThomas Gleixner */ 571b1e4d9d8SDu, Changbin static bool 572b1e4d9d8SDu, Changbin debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state), 5733ac7fe5aSThomas Gleixner void * addr, enum debug_obj_state state) 5743ac7fe5aSThomas Gleixner { 575b1e4d9d8SDu, Changbin if (fixup && fixup(addr, state)) { 576b1e4d9d8SDu, Changbin debug_objects_fixups++; 577b1e4d9d8SDu, Changbin return true; 578b1e4d9d8SDu, Changbin } 579b1e4d9d8SDu, Changbin return false; 5803ac7fe5aSThomas Gleixner } 5813ac7fe5aSThomas Gleixner 5823ac7fe5aSThomas Gleixner static void debug_object_is_on_stack(void *addr, int onstack) 5833ac7fe5aSThomas Gleixner { 5843ac7fe5aSThomas Gleixner int is_on_stack; 5853ac7fe5aSThomas Gleixner static int limit; 5863ac7fe5aSThomas Gleixner 5873ac7fe5aSThomas Gleixner if (limit > 4) 5883ac7fe5aSThomas Gleixner return; 5893ac7fe5aSThomas Gleixner 5908b05c7e6SFUJITA Tomonori is_on_stack = object_is_on_stack(addr); 5913ac7fe5aSThomas Gleixner if (is_on_stack == onstack) 5923ac7fe5aSThomas Gleixner return; 5933ac7fe5aSThomas Gleixner 5943ac7fe5aSThomas Gleixner limit++; 5953ac7fe5aSThomas Gleixner if (is_on_stack) 596fc91a3c4SJoel Fernandes (Google) pr_warn("object %p is on stack %p, but NOT annotated.\n", addr, 597fc91a3c4SJoel Fernandes (Google) task_stack_page(current)); 5983ac7fe5aSThomas Gleixner else 599fc91a3c4SJoel Fernandes (Google) pr_warn("object %p is NOT on stack %p, but annotated.\n", addr, 600fc91a3c4SJoel Fernandes (Google) task_stack_page(current)); 601fc91a3c4SJoel Fernandes (Google) 6023ac7fe5aSThomas Gleixner WARN_ON(1); 6033ac7fe5aSThomas Gleixner } 6043ac7fe5aSThomas Gleixner 60563a75969SThomas Gleixner static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b, 60663a75969SThomas Gleixner const struct debug_obj_descr *descr, 60763a75969SThomas Gleixner bool onstack, bool alloc_ifstatic) 60863a75969SThomas Gleixner { 60963a75969SThomas Gleixner struct debug_obj *obj = lookup_object(addr, b); 61063a75969SThomas Gleixner enum debug_obj_state state = ODEBUG_STATE_NONE; 61163a75969SThomas Gleixner 61263a75969SThomas Gleixner if (likely(obj)) 61363a75969SThomas Gleixner return obj; 61463a75969SThomas Gleixner 61563a75969SThomas Gleixner /* 61663a75969SThomas Gleixner * debug_object_init() unconditionally allocates untracked 61763a75969SThomas Gleixner * objects. It does not matter whether it is a static object or 61863a75969SThomas Gleixner * not. 61963a75969SThomas Gleixner * 62063a75969SThomas Gleixner * debug_object_assert_init() and debug_object_activate() allow 62163a75969SThomas Gleixner * allocation only if the descriptor callback confirms that the 62263a75969SThomas Gleixner * object is static and considered initialized. For non-static 62363a75969SThomas Gleixner * objects the allocation needs to be done from the fixup callback. 62463a75969SThomas Gleixner */ 62563a75969SThomas Gleixner if (unlikely(alloc_ifstatic)) { 62663a75969SThomas Gleixner if (!descr->is_static_object || !descr->is_static_object(addr)) 62763a75969SThomas Gleixner return ERR_PTR(-ENOENT); 62863a75969SThomas Gleixner /* Statically allocated objects are considered initialized */ 62963a75969SThomas Gleixner state = ODEBUG_STATE_INIT; 63063a75969SThomas Gleixner } 63163a75969SThomas Gleixner 63263a75969SThomas Gleixner obj = alloc_object(addr, b, descr); 63363a75969SThomas Gleixner if (likely(obj)) { 63463a75969SThomas Gleixner obj->state = state; 63563a75969SThomas Gleixner debug_object_is_on_stack(addr, onstack); 63663a75969SThomas Gleixner return obj; 63763a75969SThomas Gleixner } 63863a75969SThomas Gleixner 63963a75969SThomas Gleixner /* Out of memory. Do the cleanup outside of the locked region */ 640661cc28bSThomas Gleixner debug_objects_enabled = false; 64163a75969SThomas Gleixner return NULL; 64263a75969SThomas Gleixner } 64363a75969SThomas Gleixner 6440af462f1SThomas Gleixner static void debug_objects_fill_pool(void) 6450af462f1SThomas Gleixner { 64614077b9eSThomas Gleixner if (!static_branch_likely(&obj_cache_enabled)) 647d8c6cd3aSZhen Lei return; 648d8c6cd3aSZhen Lei 64996a9a042SThomas Gleixner if (likely(!pool_should_refill(&pool_global))) 650d8c6cd3aSZhen Lei return; 651d8c6cd3aSZhen Lei 652d8c6cd3aSZhen Lei /* Try reusing objects from obj_to_free_list */ 653d8c6cd3aSZhen Lei fill_pool_from_freelist(); 654d8c6cd3aSZhen Lei 65596a9a042SThomas Gleixner if (likely(!pool_should_refill(&pool_global))) 656d8c6cd3aSZhen Lei return; 657d8c6cd3aSZhen Lei 6580af462f1SThomas Gleixner /* 6590af462f1SThomas Gleixner * On RT enabled kernels the pool refill must happen in preemptible 6600cce06baSPeter Zijlstra * context -- for !RT kernels we rely on the fact that spinlock_t and 6610cce06baSPeter Zijlstra * raw_spinlock_t are basically the same type and this lock-type 6620cce06baSPeter Zijlstra * inversion works just fine. 6630af462f1SThomas Gleixner */ 6640cce06baSPeter Zijlstra if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible()) { 6650cce06baSPeter Zijlstra /* 6660cce06baSPeter Zijlstra * Annotate away the spinlock_t inside raw_spinlock_t warning 6670cce06baSPeter Zijlstra * by temporarily raising the wait-type to WAIT_SLEEP, matching 6680cce06baSPeter Zijlstra * the preemptible() condition above. 6690cce06baSPeter Zijlstra */ 6700cce06baSPeter Zijlstra static DEFINE_WAIT_OVERRIDE_MAP(fill_pool_map, LD_WAIT_SLEEP); 6710cce06baSPeter Zijlstra lock_map_acquire_try(&fill_pool_map); 6720af462f1SThomas Gleixner fill_pool(); 6730cce06baSPeter Zijlstra lock_map_release(&fill_pool_map); 6740cce06baSPeter Zijlstra } 6750af462f1SThomas Gleixner } 6760af462f1SThomas Gleixner 6773ac7fe5aSThomas Gleixner static void 678aedcade6SStephen Boyd __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack) 6793ac7fe5aSThomas Gleixner { 6809bb63626SAndrzej Hajda struct debug_obj *obj, o; 6813ac7fe5aSThomas Gleixner struct debug_bucket *db; 6823ac7fe5aSThomas Gleixner unsigned long flags; 6833ac7fe5aSThomas Gleixner 6840af462f1SThomas Gleixner debug_objects_fill_pool(); 68550db04ddSVegard Nossum 6863ac7fe5aSThomas Gleixner db = get_bucket((unsigned long) addr); 6873ac7fe5aSThomas Gleixner 688aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 6893ac7fe5aSThomas Gleixner 69063a75969SThomas Gleixner obj = lookup_object_or_alloc(addr, db, descr, onstack, false); 69163a75969SThomas Gleixner if (unlikely(!obj)) { 692aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 6933ac7fe5aSThomas Gleixner debug_objects_oom(); 6943ac7fe5aSThomas Gleixner return; 6953ac7fe5aSThomas Gleixner } 6963ac7fe5aSThomas Gleixner 6973ac7fe5aSThomas Gleixner switch (obj->state) { 6983ac7fe5aSThomas Gleixner case ODEBUG_STATE_NONE: 6993ac7fe5aSThomas Gleixner case ODEBUG_STATE_INIT: 7003ac7fe5aSThomas Gleixner case ODEBUG_STATE_INACTIVE: 7013ac7fe5aSThomas Gleixner obj->state = ODEBUG_STATE_INIT; 702aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 703d5f34153SWaiman Long return; 7043ac7fe5aSThomas Gleixner default: 7053ac7fe5aSThomas Gleixner break; 7063ac7fe5aSThomas Gleixner } 7073ac7fe5aSThomas Gleixner 7089bb63626SAndrzej Hajda o = *obj; 709aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 7109bb63626SAndrzej Hajda debug_print_object(&o, "init"); 7119bb63626SAndrzej Hajda 7129bb63626SAndrzej Hajda if (o.state == ODEBUG_STATE_ACTIVE) 7139bb63626SAndrzej Hajda debug_object_fixup(descr->fixup_init, addr, o.state); 7143ac7fe5aSThomas Gleixner } 7153ac7fe5aSThomas Gleixner 7163ac7fe5aSThomas Gleixner /** 7173ac7fe5aSThomas Gleixner * debug_object_init - debug checks when an object is initialized 7183ac7fe5aSThomas Gleixner * @addr: address of the object 7193ac7fe5aSThomas Gleixner * @descr: pointer to an object specific debug description structure 7203ac7fe5aSThomas Gleixner */ 721aedcade6SStephen Boyd void debug_object_init(void *addr, const struct debug_obj_descr *descr) 7223ac7fe5aSThomas Gleixner { 7233ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 7243ac7fe5aSThomas Gleixner return; 7253ac7fe5aSThomas Gleixner 7263ac7fe5aSThomas Gleixner __debug_object_init(addr, descr, 0); 7273ac7fe5aSThomas Gleixner } 728f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init); 7293ac7fe5aSThomas Gleixner 7303ac7fe5aSThomas Gleixner /** 7313ac7fe5aSThomas Gleixner * debug_object_init_on_stack - debug checks when an object on stack is 7323ac7fe5aSThomas Gleixner * initialized 7333ac7fe5aSThomas Gleixner * @addr: address of the object 7343ac7fe5aSThomas Gleixner * @descr: pointer to an object specific debug description structure 7353ac7fe5aSThomas Gleixner */ 736aedcade6SStephen Boyd void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr) 7373ac7fe5aSThomas Gleixner { 7383ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 7393ac7fe5aSThomas Gleixner return; 7403ac7fe5aSThomas Gleixner 7413ac7fe5aSThomas Gleixner __debug_object_init(addr, descr, 1); 7423ac7fe5aSThomas Gleixner } 743f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init_on_stack); 7443ac7fe5aSThomas Gleixner 7453ac7fe5aSThomas Gleixner /** 7463ac7fe5aSThomas Gleixner * debug_object_activate - debug checks when an object is activated 7473ac7fe5aSThomas Gleixner * @addr: address of the object 7483ac7fe5aSThomas Gleixner * @descr: pointer to an object specific debug description structure 749b778ae25SPaul E. McKenney * Returns 0 for success, -EINVAL for check failed. 7503ac7fe5aSThomas Gleixner */ 751aedcade6SStephen Boyd int debug_object_activate(void *addr, const struct debug_obj_descr *descr) 7523ac7fe5aSThomas Gleixner { 75363a75969SThomas Gleixner struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr }; 7543ac7fe5aSThomas Gleixner struct debug_bucket *db; 7553ac7fe5aSThomas Gleixner struct debug_obj *obj; 7563ac7fe5aSThomas Gleixner unsigned long flags; 7573ac7fe5aSThomas Gleixner 7583ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 759b778ae25SPaul E. McKenney return 0; 7603ac7fe5aSThomas Gleixner 7610af462f1SThomas Gleixner debug_objects_fill_pool(); 7620af462f1SThomas Gleixner 7633ac7fe5aSThomas Gleixner db = get_bucket((unsigned long) addr); 7643ac7fe5aSThomas Gleixner 765aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 7663ac7fe5aSThomas Gleixner 76763a75969SThomas Gleixner obj = lookup_object_or_alloc(addr, db, descr, false, true); 7689bb63626SAndrzej Hajda if (unlikely(!obj)) { 7699bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 7709bb63626SAndrzej Hajda debug_objects_oom(); 7719bb63626SAndrzej Hajda return 0; 7729bb63626SAndrzej Hajda } else if (likely(!IS_ERR(obj))) { 7733ac7fe5aSThomas Gleixner switch (obj->state) { 7749bb63626SAndrzej Hajda case ODEBUG_STATE_ACTIVE: 7759bb63626SAndrzej Hajda case ODEBUG_STATE_DESTROYED: 7769bb63626SAndrzej Hajda o = *obj; 7779bb63626SAndrzej Hajda break; 7783ac7fe5aSThomas Gleixner case ODEBUG_STATE_INIT: 7793ac7fe5aSThomas Gleixner case ODEBUG_STATE_INACTIVE: 7803ac7fe5aSThomas Gleixner obj->state = ODEBUG_STATE_ACTIVE; 7819bb63626SAndrzej Hajda fallthrough; 7823ac7fe5aSThomas Gleixner default: 783aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 784b778ae25SPaul E. McKenney return 0; 7853ac7fe5aSThomas Gleixner } 7869bb63626SAndrzej Hajda } 78763a75969SThomas Gleixner 7889bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 78963a75969SThomas Gleixner debug_print_object(&o, "activate"); 7909bb63626SAndrzej Hajda 7919bb63626SAndrzej Hajda switch (o.state) { 7929bb63626SAndrzej Hajda case ODEBUG_STATE_ACTIVE: 7939bb63626SAndrzej Hajda case ODEBUG_STATE_NOTAVAILABLE: 7949bb63626SAndrzej Hajda if (debug_object_fixup(descr->fixup_activate, addr, o.state)) 7959bb63626SAndrzej Hajda return 0; 7969bb63626SAndrzej Hajda fallthrough; 7979bb63626SAndrzej Hajda default: 7989bb63626SAndrzej Hajda return -EINVAL; 7999bb63626SAndrzej Hajda } 80063a75969SThomas Gleixner } 801f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_activate); 8023ac7fe5aSThomas Gleixner 8033ac7fe5aSThomas Gleixner /** 8043ac7fe5aSThomas Gleixner * debug_object_deactivate - debug checks when an object is deactivated 8053ac7fe5aSThomas Gleixner * @addr: address of the object 8063ac7fe5aSThomas Gleixner * @descr: pointer to an object specific debug description structure 8073ac7fe5aSThomas Gleixner */ 808aedcade6SStephen Boyd void debug_object_deactivate(void *addr, const struct debug_obj_descr *descr) 8093ac7fe5aSThomas Gleixner { 8109bb63626SAndrzej Hajda struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr }; 8113ac7fe5aSThomas Gleixner struct debug_bucket *db; 8123ac7fe5aSThomas Gleixner struct debug_obj *obj; 8133ac7fe5aSThomas Gleixner unsigned long flags; 8143ac7fe5aSThomas Gleixner 8153ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 8163ac7fe5aSThomas Gleixner return; 8173ac7fe5aSThomas Gleixner 8183ac7fe5aSThomas Gleixner db = get_bucket((unsigned long) addr); 8193ac7fe5aSThomas Gleixner 820aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 8213ac7fe5aSThomas Gleixner 8223ac7fe5aSThomas Gleixner obj = lookup_object(addr, db); 8233ac7fe5aSThomas Gleixner if (obj) { 8243ac7fe5aSThomas Gleixner switch (obj->state) { 8259bb63626SAndrzej Hajda case ODEBUG_STATE_DESTROYED: 8269bb63626SAndrzej Hajda break; 8273ac7fe5aSThomas Gleixner case ODEBUG_STATE_INIT: 8283ac7fe5aSThomas Gleixner case ODEBUG_STATE_INACTIVE: 8293ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 8309bb63626SAndrzej Hajda if (obj->astate) 8319bb63626SAndrzej Hajda break; 8323ac7fe5aSThomas Gleixner obj->state = ODEBUG_STATE_INACTIVE; 8339bb63626SAndrzej Hajda fallthrough; 8343ac7fe5aSThomas Gleixner default: 8359bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 8369bb63626SAndrzej Hajda return; 8373ac7fe5aSThomas Gleixner } 8389bb63626SAndrzej Hajda o = *obj; 839d5f34153SWaiman Long } 840d5f34153SWaiman Long 841d5f34153SWaiman Long raw_spin_unlock_irqrestore(&db->lock, flags); 8423ac7fe5aSThomas Gleixner debug_print_object(&o, "deactivate"); 8433ac7fe5aSThomas Gleixner } 844f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_deactivate); 8453ac7fe5aSThomas Gleixner 8463ac7fe5aSThomas Gleixner /** 8473ac7fe5aSThomas Gleixner * debug_object_destroy - debug checks when an object is destroyed 8483ac7fe5aSThomas Gleixner * @addr: address of the object 8493ac7fe5aSThomas Gleixner * @descr: pointer to an object specific debug description structure 8503ac7fe5aSThomas Gleixner */ 851aedcade6SStephen Boyd void debug_object_destroy(void *addr, const struct debug_obj_descr *descr) 8523ac7fe5aSThomas Gleixner { 8539bb63626SAndrzej Hajda struct debug_obj *obj, o; 8543ac7fe5aSThomas Gleixner struct debug_bucket *db; 8553ac7fe5aSThomas Gleixner unsigned long flags; 8563ac7fe5aSThomas Gleixner 8573ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 8583ac7fe5aSThomas Gleixner return; 8593ac7fe5aSThomas Gleixner 8603ac7fe5aSThomas Gleixner db = get_bucket((unsigned long) addr); 8613ac7fe5aSThomas Gleixner 862aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 8633ac7fe5aSThomas Gleixner 8643ac7fe5aSThomas Gleixner obj = lookup_object(addr, db); 8659bb63626SAndrzej Hajda if (!obj) { 8669bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 8679bb63626SAndrzej Hajda return; 8689bb63626SAndrzej Hajda } 8693ac7fe5aSThomas Gleixner 8703ac7fe5aSThomas Gleixner switch (obj->state) { 8719bb63626SAndrzej Hajda case ODEBUG_STATE_ACTIVE: 8729bb63626SAndrzej Hajda case ODEBUG_STATE_DESTROYED: 8739bb63626SAndrzej Hajda break; 8743ac7fe5aSThomas Gleixner case ODEBUG_STATE_NONE: 8753ac7fe5aSThomas Gleixner case ODEBUG_STATE_INIT: 8763ac7fe5aSThomas Gleixner case ODEBUG_STATE_INACTIVE: 8773ac7fe5aSThomas Gleixner obj->state = ODEBUG_STATE_DESTROYED; 8789bb63626SAndrzej Hajda fallthrough; 8793ac7fe5aSThomas Gleixner default: 880aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 8819bb63626SAndrzej Hajda return; 8829bb63626SAndrzej Hajda } 8839bb63626SAndrzej Hajda 8849bb63626SAndrzej Hajda o = *obj; 8859bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 8869bb63626SAndrzej Hajda debug_print_object(&o, "destroy"); 8879bb63626SAndrzej Hajda 8889bb63626SAndrzej Hajda if (o.state == ODEBUG_STATE_ACTIVE) 8899bb63626SAndrzej Hajda debug_object_fixup(descr->fixup_destroy, addr, o.state); 8903ac7fe5aSThomas Gleixner } 891f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_destroy); 8923ac7fe5aSThomas Gleixner 8933ac7fe5aSThomas Gleixner /** 8943ac7fe5aSThomas Gleixner * debug_object_free - debug checks when an object is freed 8953ac7fe5aSThomas Gleixner * @addr: address of the object 8963ac7fe5aSThomas Gleixner * @descr: pointer to an object specific debug description structure 8973ac7fe5aSThomas Gleixner */ 898aedcade6SStephen Boyd void debug_object_free(void *addr, const struct debug_obj_descr *descr) 8993ac7fe5aSThomas Gleixner { 9009bb63626SAndrzej Hajda struct debug_obj *obj, o; 9013ac7fe5aSThomas Gleixner struct debug_bucket *db; 9023ac7fe5aSThomas Gleixner unsigned long flags; 9033ac7fe5aSThomas Gleixner 9043ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 9053ac7fe5aSThomas Gleixner return; 9063ac7fe5aSThomas Gleixner 9073ac7fe5aSThomas Gleixner db = get_bucket((unsigned long) addr); 9083ac7fe5aSThomas Gleixner 909aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 9103ac7fe5aSThomas Gleixner 9113ac7fe5aSThomas Gleixner obj = lookup_object(addr, db); 9129bb63626SAndrzej Hajda if (!obj) { 9139bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 9149bb63626SAndrzej Hajda return; 9159bb63626SAndrzej Hajda } 9163ac7fe5aSThomas Gleixner 9173ac7fe5aSThomas Gleixner switch (obj->state) { 9183ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 9199bb63626SAndrzej Hajda break; 9203ac7fe5aSThomas Gleixner default: 9213ac7fe5aSThomas Gleixner hlist_del(&obj->node); 922aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 9233ac7fe5aSThomas Gleixner free_object(obj); 924673d62ccSVegard Nossum return; 9253ac7fe5aSThomas Gleixner } 9269bb63626SAndrzej Hajda 9279bb63626SAndrzej Hajda o = *obj; 928aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 9299bb63626SAndrzej Hajda debug_print_object(&o, "free"); 9309bb63626SAndrzej Hajda 9319bb63626SAndrzej Hajda debug_object_fixup(descr->fixup_free, addr, o.state); 9323ac7fe5aSThomas Gleixner } 933f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_free); 9343ac7fe5aSThomas Gleixner 935a5d8e467SMathieu Desnoyers /** 936b84d435cSChristine Chan * debug_object_assert_init - debug checks when object should be init-ed 937b84d435cSChristine Chan * @addr: address of the object 938b84d435cSChristine Chan * @descr: pointer to an object specific debug description structure 939b84d435cSChristine Chan */ 940aedcade6SStephen Boyd void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr) 941b84d435cSChristine Chan { 94263a75969SThomas Gleixner struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr }; 943b84d435cSChristine Chan struct debug_bucket *db; 944b84d435cSChristine Chan struct debug_obj *obj; 945b84d435cSChristine Chan unsigned long flags; 946b84d435cSChristine Chan 947b84d435cSChristine Chan if (!debug_objects_enabled) 948b84d435cSChristine Chan return; 949b84d435cSChristine Chan 9500af462f1SThomas Gleixner debug_objects_fill_pool(); 9510af462f1SThomas Gleixner 952b84d435cSChristine Chan db = get_bucket((unsigned long) addr); 953b84d435cSChristine Chan 954b84d435cSChristine Chan raw_spin_lock_irqsave(&db->lock, flags); 95563a75969SThomas Gleixner obj = lookup_object_or_alloc(addr, db, descr, false, true); 956b84d435cSChristine Chan raw_spin_unlock_irqrestore(&db->lock, flags); 95763a75969SThomas Gleixner if (likely(!IS_ERR_OR_NULL(obj))) 95863a75969SThomas Gleixner return; 95963a75969SThomas Gleixner 96063a75969SThomas Gleixner /* If NULL the allocation has hit OOM */ 96163a75969SThomas Gleixner if (!obj) { 96263a75969SThomas Gleixner debug_objects_oom(); 963b84d435cSChristine Chan return; 964b84d435cSChristine Chan } 965b84d435cSChristine Chan 96663a75969SThomas Gleixner /* Object is neither tracked nor static. It's not initialized. */ 96763a75969SThomas Gleixner debug_print_object(&o, "assert_init"); 96863a75969SThomas Gleixner debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE); 969b84d435cSChristine Chan } 970f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_assert_init); 971b84d435cSChristine Chan 972b84d435cSChristine Chan /** 973a5d8e467SMathieu Desnoyers * debug_object_active_state - debug checks object usage state machine 974a5d8e467SMathieu Desnoyers * @addr: address of the object 975a5d8e467SMathieu Desnoyers * @descr: pointer to an object specific debug description structure 976a5d8e467SMathieu Desnoyers * @expect: expected state 977a5d8e467SMathieu Desnoyers * @next: state to move to if expected state is found 978a5d8e467SMathieu Desnoyers */ 979a5d8e467SMathieu Desnoyers void 980aedcade6SStephen Boyd debug_object_active_state(void *addr, const struct debug_obj_descr *descr, 981a5d8e467SMathieu Desnoyers unsigned int expect, unsigned int next) 982a5d8e467SMathieu Desnoyers { 9839bb63626SAndrzej Hajda struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr }; 984a5d8e467SMathieu Desnoyers struct debug_bucket *db; 985a5d8e467SMathieu Desnoyers struct debug_obj *obj; 986a5d8e467SMathieu Desnoyers unsigned long flags; 987a5d8e467SMathieu Desnoyers 988a5d8e467SMathieu Desnoyers if (!debug_objects_enabled) 989a5d8e467SMathieu Desnoyers return; 990a5d8e467SMathieu Desnoyers 991a5d8e467SMathieu Desnoyers db = get_bucket((unsigned long) addr); 992a5d8e467SMathieu Desnoyers 993a5d8e467SMathieu Desnoyers raw_spin_lock_irqsave(&db->lock, flags); 994a5d8e467SMathieu Desnoyers 995a5d8e467SMathieu Desnoyers obj = lookup_object(addr, db); 996a5d8e467SMathieu Desnoyers if (obj) { 997a5d8e467SMathieu Desnoyers switch (obj->state) { 998a5d8e467SMathieu Desnoyers case ODEBUG_STATE_ACTIVE: 9999bb63626SAndrzej Hajda if (obj->astate != expect) 1000a5d8e467SMathieu Desnoyers break; 10019bb63626SAndrzej Hajda obj->astate = next; 10029bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 10039bb63626SAndrzej Hajda return; 1004a5d8e467SMathieu Desnoyers default: 1005a5d8e467SMathieu Desnoyers break; 1006a5d8e467SMathieu Desnoyers } 10079bb63626SAndrzej Hajda o = *obj; 1008d5f34153SWaiman Long } 1009d5f34153SWaiman Long 1010d5f34153SWaiman Long raw_spin_unlock_irqrestore(&db->lock, flags); 1011a5d8e467SMathieu Desnoyers debug_print_object(&o, "active_state"); 1012a5d8e467SMathieu Desnoyers } 1013f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_active_state); 1014a5d8e467SMathieu Desnoyers 10153ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE 10163ac7fe5aSThomas Gleixner static void __debug_check_no_obj_freed(const void *address, unsigned long size) 10173ac7fe5aSThomas Gleixner { 10183ac7fe5aSThomas Gleixner unsigned long flags, oaddr, saddr, eaddr, paddr, chunks; 10199bb63626SAndrzej Hajda int cnt, objs_checked = 0; 10209bb63626SAndrzej Hajda struct debug_obj *obj, o; 10213ac7fe5aSThomas Gleixner struct debug_bucket *db; 10221ea9b98bSYang Shi struct hlist_node *tmp; 10233ac7fe5aSThomas Gleixner 10243ac7fe5aSThomas Gleixner saddr = (unsigned long) address; 10253ac7fe5aSThomas Gleixner eaddr = saddr + size; 10263ac7fe5aSThomas Gleixner paddr = saddr & ODEBUG_CHUNK_MASK; 10273ac7fe5aSThomas Gleixner chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1)); 10283ac7fe5aSThomas Gleixner chunks >>= ODEBUG_CHUNK_SHIFT; 10293ac7fe5aSThomas Gleixner 10303ac7fe5aSThomas Gleixner for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) { 10313ac7fe5aSThomas Gleixner db = get_bucket(paddr); 10323ac7fe5aSThomas Gleixner 10333ac7fe5aSThomas Gleixner repeat: 10343ac7fe5aSThomas Gleixner cnt = 0; 1035aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 1036b67bfe0dSSasha Levin hlist_for_each_entry_safe(obj, tmp, &db->list, node) { 10373ac7fe5aSThomas Gleixner cnt++; 10383ac7fe5aSThomas Gleixner oaddr = (unsigned long) obj->object; 10393ac7fe5aSThomas Gleixner if (oaddr < saddr || oaddr >= eaddr) 10403ac7fe5aSThomas Gleixner continue; 10413ac7fe5aSThomas Gleixner 10423ac7fe5aSThomas Gleixner switch (obj->state) { 10433ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 10449bb63626SAndrzej Hajda o = *obj; 1045aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 10469bb63626SAndrzej Hajda debug_print_object(&o, "free"); 10479bb63626SAndrzej Hajda debug_object_fixup(o.descr->fixup_free, (void *)oaddr, o.state); 10483ac7fe5aSThomas Gleixner goto repeat; 10493ac7fe5aSThomas Gleixner default: 10503ac7fe5aSThomas Gleixner hlist_del(&obj->node); 1051a7344a68SWaiman Long __free_object(obj); 10523ac7fe5aSThomas Gleixner break; 10533ac7fe5aSThomas Gleixner } 10543ac7fe5aSThomas Gleixner } 1055aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 1056673d62ccSVegard Nossum 10573ac7fe5aSThomas Gleixner if (cnt > debug_objects_maxchain) 10583ac7fe5aSThomas Gleixner debug_objects_maxchain = cnt; 1059bd9dcd04SYang Shi 1060bd9dcd04SYang Shi objs_checked += cnt; 10613ac7fe5aSThomas Gleixner } 1062bd9dcd04SYang Shi 1063bd9dcd04SYang Shi if (objs_checked > debug_objects_maxchecked) 1064bd9dcd04SYang Shi debug_objects_maxchecked = objs_checked; 10651ea9b98bSYang Shi 10661ea9b98bSYang Shi /* Schedule work to actually kmem_cache_free() objects */ 1067e18328ffSThomas Gleixner if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) { 1068a7344a68SWaiman Long WRITE_ONCE(obj_freeing, true); 1069a7344a68SWaiman Long schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY); 1070a7344a68SWaiman Long } 10713ac7fe5aSThomas Gleixner } 10723ac7fe5aSThomas Gleixner 10733ac7fe5aSThomas Gleixner void debug_check_no_obj_freed(const void *address, unsigned long size) 10743ac7fe5aSThomas Gleixner { 10753ac7fe5aSThomas Gleixner if (debug_objects_enabled) 10763ac7fe5aSThomas Gleixner __debug_check_no_obj_freed(address, size); 10773ac7fe5aSThomas Gleixner } 10783ac7fe5aSThomas Gleixner #endif 10793ac7fe5aSThomas Gleixner 10803ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_FS 10813ac7fe5aSThomas Gleixner 10823ac7fe5aSThomas Gleixner static int debug_stats_show(struct seq_file *m, void *v) 10833ac7fe5aSThomas Gleixner { 10842638345dSThomas Gleixner unsigned int cpu, pool_used, pcp_free = 0; 1085d86998b1SWaiman Long 10862638345dSThomas Gleixner /* 10872638345dSThomas Gleixner * pool_global.stats.cur_used is the number of batches currently 10882638345dSThomas Gleixner * handed out to per CPU pools. Convert it to number of objects 10892638345dSThomas Gleixner * and subtract the number of free objects in the per CPU pools. 10902638345dSThomas Gleixner * As this is lockless the number is an estimate. 10912638345dSThomas Gleixner */ 1092d86998b1SWaiman Long for_each_possible_cpu(cpu) 10932638345dSThomas Gleixner pcp_free += per_cpu(pool_pcpu.cnt, cpu); 10942638345dSThomas Gleixner 10952638345dSThomas Gleixner pool_used = data_race(pool_global.stats.cur_used); 10962638345dSThomas Gleixner pcp_free = min(pool_used, pcp_free); 10972638345dSThomas Gleixner pool_used -= pcp_free; 1098d86998b1SWaiman Long 10993ac7fe5aSThomas Gleixner seq_printf(m, "max_chain : %d\n", debug_objects_maxchain); 1100bd9dcd04SYang Shi seq_printf(m, "max_checked : %d\n", debug_objects_maxchecked); 11013ac7fe5aSThomas Gleixner seq_printf(m, "warnings : %d\n", debug_objects_warnings); 11023ac7fe5aSThomas Gleixner seq_printf(m, "fixups : %d\n", debug_objects_fixups); 11032638345dSThomas Gleixner seq_printf(m, "pool_free : %u\n", pool_count(&pool_global) + pcp_free); 11042638345dSThomas Gleixner seq_printf(m, "pool_pcp_free : %u\n", pcp_free); 11052638345dSThomas Gleixner seq_printf(m, "pool_min_free : %u\n", data_race(pool_global.stats.min_fill)); 11062638345dSThomas Gleixner seq_printf(m, "pool_used : %u\n", pool_used); 11072638345dSThomas Gleixner seq_printf(m, "pool_max_used : %u\n", data_race(pool_global.stats.max_used)); 11082638345dSThomas Gleixner seq_printf(m, "on_free_list : %u\n", pool_count(&pool_to_free)); 11090cad93c3SWaiman Long seq_printf(m, "objs_allocated: %d\n", debug_objects_allocated); 11100cad93c3SWaiman Long seq_printf(m, "objs_freed : %d\n", debug_objects_freed); 11113ac7fe5aSThomas Gleixner return 0; 11123ac7fe5aSThomas Gleixner } 11130f85c480SQinglang Miao DEFINE_SHOW_ATTRIBUTE(debug_stats); 11143ac7fe5aSThomas Gleixner 11153ac7fe5aSThomas Gleixner static int __init debug_objects_init_debugfs(void) 11163ac7fe5aSThomas Gleixner { 1117fecb0d95SGreg Kroah-Hartman struct dentry *dbgdir; 11183ac7fe5aSThomas Gleixner 11193ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 11203ac7fe5aSThomas Gleixner return 0; 11213ac7fe5aSThomas Gleixner 11223ac7fe5aSThomas Gleixner dbgdir = debugfs_create_dir("debug_objects", NULL); 11233ac7fe5aSThomas Gleixner 1124fecb0d95SGreg Kroah-Hartman debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops); 11253ac7fe5aSThomas Gleixner 11263ac7fe5aSThomas Gleixner return 0; 11273ac7fe5aSThomas Gleixner } 11283ac7fe5aSThomas Gleixner __initcall(debug_objects_init_debugfs); 11293ac7fe5aSThomas Gleixner 11303ac7fe5aSThomas Gleixner #else 11313ac7fe5aSThomas Gleixner static inline void debug_objects_init_debugfs(void) { } 11323ac7fe5aSThomas Gleixner #endif 11333ac7fe5aSThomas Gleixner 11343ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST 11353ac7fe5aSThomas Gleixner 11363ac7fe5aSThomas Gleixner /* Random data structure for the self test */ 11373ac7fe5aSThomas Gleixner struct self_test { 11383ac7fe5aSThomas Gleixner unsigned long dummy1[6]; 11393ac7fe5aSThomas Gleixner int static_init; 11403ac7fe5aSThomas Gleixner unsigned long dummy2[3]; 11413ac7fe5aSThomas Gleixner }; 11423ac7fe5aSThomas Gleixner 1143aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test; 11443ac7fe5aSThomas Gleixner 1145b9fdac7fSDu, Changbin static bool __init is_static_object(void *addr) 1146b9fdac7fSDu, Changbin { 1147b9fdac7fSDu, Changbin struct self_test *obj = addr; 1148b9fdac7fSDu, Changbin 1149b9fdac7fSDu, Changbin return obj->static_init; 1150b9fdac7fSDu, Changbin } 1151b9fdac7fSDu, Changbin 11523ac7fe5aSThomas Gleixner /* 11533ac7fe5aSThomas Gleixner * fixup_init is called when: 11543ac7fe5aSThomas Gleixner * - an active object is initialized 11553ac7fe5aSThomas Gleixner */ 1156b1e4d9d8SDu, Changbin static bool __init fixup_init(void *addr, enum debug_obj_state state) 11573ac7fe5aSThomas Gleixner { 11583ac7fe5aSThomas Gleixner struct self_test *obj = addr; 11593ac7fe5aSThomas Gleixner 11603ac7fe5aSThomas Gleixner switch (state) { 11613ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 11623ac7fe5aSThomas Gleixner debug_object_deactivate(obj, &descr_type_test); 11633ac7fe5aSThomas Gleixner debug_object_init(obj, &descr_type_test); 1164b1e4d9d8SDu, Changbin return true; 11653ac7fe5aSThomas Gleixner default: 1166b1e4d9d8SDu, Changbin return false; 11673ac7fe5aSThomas Gleixner } 11683ac7fe5aSThomas Gleixner } 11693ac7fe5aSThomas Gleixner 11703ac7fe5aSThomas Gleixner /* 11713ac7fe5aSThomas Gleixner * fixup_activate is called when: 11723ac7fe5aSThomas Gleixner * - an active object is activated 1173b9fdac7fSDu, Changbin * - an unknown non-static object is activated 11743ac7fe5aSThomas Gleixner */ 1175b1e4d9d8SDu, Changbin static bool __init fixup_activate(void *addr, enum debug_obj_state state) 11763ac7fe5aSThomas Gleixner { 11773ac7fe5aSThomas Gleixner struct self_test *obj = addr; 11783ac7fe5aSThomas Gleixner 11793ac7fe5aSThomas Gleixner switch (state) { 11803ac7fe5aSThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 1181b1e4d9d8SDu, Changbin return true; 11823ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 11833ac7fe5aSThomas Gleixner debug_object_deactivate(obj, &descr_type_test); 11843ac7fe5aSThomas Gleixner debug_object_activate(obj, &descr_type_test); 1185b1e4d9d8SDu, Changbin return true; 11863ac7fe5aSThomas Gleixner 11873ac7fe5aSThomas Gleixner default: 1188b1e4d9d8SDu, Changbin return false; 11893ac7fe5aSThomas Gleixner } 11903ac7fe5aSThomas Gleixner } 11913ac7fe5aSThomas Gleixner 11923ac7fe5aSThomas Gleixner /* 11933ac7fe5aSThomas Gleixner * fixup_destroy is called when: 11943ac7fe5aSThomas Gleixner * - an active object is destroyed 11953ac7fe5aSThomas Gleixner */ 1196b1e4d9d8SDu, Changbin static bool __init fixup_destroy(void *addr, enum debug_obj_state state) 11973ac7fe5aSThomas Gleixner { 11983ac7fe5aSThomas Gleixner struct self_test *obj = addr; 11993ac7fe5aSThomas Gleixner 12003ac7fe5aSThomas Gleixner switch (state) { 12013ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 12023ac7fe5aSThomas Gleixner debug_object_deactivate(obj, &descr_type_test); 12033ac7fe5aSThomas Gleixner debug_object_destroy(obj, &descr_type_test); 1204b1e4d9d8SDu, Changbin return true; 12053ac7fe5aSThomas Gleixner default: 1206b1e4d9d8SDu, Changbin return false; 12073ac7fe5aSThomas Gleixner } 12083ac7fe5aSThomas Gleixner } 12093ac7fe5aSThomas Gleixner 12103ac7fe5aSThomas Gleixner /* 12113ac7fe5aSThomas Gleixner * fixup_free is called when: 12123ac7fe5aSThomas Gleixner * - an active object is freed 12133ac7fe5aSThomas Gleixner */ 1214b1e4d9d8SDu, Changbin static bool __init fixup_free(void *addr, enum debug_obj_state state) 12153ac7fe5aSThomas Gleixner { 12163ac7fe5aSThomas Gleixner struct self_test *obj = addr; 12173ac7fe5aSThomas Gleixner 12183ac7fe5aSThomas Gleixner switch (state) { 12193ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 12203ac7fe5aSThomas Gleixner debug_object_deactivate(obj, &descr_type_test); 12213ac7fe5aSThomas Gleixner debug_object_free(obj, &descr_type_test); 1222b1e4d9d8SDu, Changbin return true; 12233ac7fe5aSThomas Gleixner default: 1224b1e4d9d8SDu, Changbin return false; 12253ac7fe5aSThomas Gleixner } 12263ac7fe5aSThomas Gleixner } 12273ac7fe5aSThomas Gleixner 12281fb2f77cSHenrik Kretzschmar static int __init 12293ac7fe5aSThomas Gleixner check_results(void *addr, enum debug_obj_state state, int fixups, int warnings) 12303ac7fe5aSThomas Gleixner { 12313ac7fe5aSThomas Gleixner struct debug_bucket *db; 12323ac7fe5aSThomas Gleixner struct debug_obj *obj; 12333ac7fe5aSThomas Gleixner unsigned long flags; 12343ac7fe5aSThomas Gleixner int res = -EINVAL; 12353ac7fe5aSThomas Gleixner 12363ac7fe5aSThomas Gleixner db = get_bucket((unsigned long) addr); 12373ac7fe5aSThomas Gleixner 1238aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 12393ac7fe5aSThomas Gleixner 12403ac7fe5aSThomas Gleixner obj = lookup_object(addr, db); 12413ac7fe5aSThomas Gleixner if (!obj && state != ODEBUG_STATE_NONE) { 12425cd2b459SArjan van de Ven WARN(1, KERN_ERR "ODEBUG: selftest object not found\n"); 12433ac7fe5aSThomas Gleixner goto out; 12443ac7fe5aSThomas Gleixner } 12453ac7fe5aSThomas Gleixner if (obj && obj->state != state) { 12465cd2b459SArjan van de Ven WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n", 12473ac7fe5aSThomas Gleixner obj->state, state); 12483ac7fe5aSThomas Gleixner goto out; 12493ac7fe5aSThomas Gleixner } 12503ac7fe5aSThomas Gleixner if (fixups != debug_objects_fixups) { 12515cd2b459SArjan van de Ven WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n", 12523ac7fe5aSThomas Gleixner fixups, debug_objects_fixups); 12533ac7fe5aSThomas Gleixner goto out; 12543ac7fe5aSThomas Gleixner } 12553ac7fe5aSThomas Gleixner if (warnings != debug_objects_warnings) { 12565cd2b459SArjan van de Ven WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n", 12573ac7fe5aSThomas Gleixner warnings, debug_objects_warnings); 12583ac7fe5aSThomas Gleixner goto out; 12593ac7fe5aSThomas Gleixner } 12603ac7fe5aSThomas Gleixner res = 0; 12613ac7fe5aSThomas Gleixner out: 1262aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 12633ac7fe5aSThomas Gleixner if (res) 1264661cc28bSThomas Gleixner debug_objects_enabled = false; 12653ac7fe5aSThomas Gleixner return res; 12663ac7fe5aSThomas Gleixner } 12673ac7fe5aSThomas Gleixner 1268aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test = { 12693ac7fe5aSThomas Gleixner .name = "selftest", 1270b9fdac7fSDu, Changbin .is_static_object = is_static_object, 12713ac7fe5aSThomas Gleixner .fixup_init = fixup_init, 12723ac7fe5aSThomas Gleixner .fixup_activate = fixup_activate, 12733ac7fe5aSThomas Gleixner .fixup_destroy = fixup_destroy, 12743ac7fe5aSThomas Gleixner .fixup_free = fixup_free, 12753ac7fe5aSThomas Gleixner }; 12763ac7fe5aSThomas Gleixner 12773ac7fe5aSThomas Gleixner static __initdata struct self_test obj = { .static_init = 0 }; 12783ac7fe5aSThomas Gleixner 127955fb412eSThomas Gleixner static bool __init debug_objects_selftest(void) 12803ac7fe5aSThomas Gleixner { 12813ac7fe5aSThomas Gleixner int fixups, oldfixups, warnings, oldwarnings; 12823ac7fe5aSThomas Gleixner unsigned long flags; 12833ac7fe5aSThomas Gleixner 12843ac7fe5aSThomas Gleixner local_irq_save(flags); 12853ac7fe5aSThomas Gleixner 12863ac7fe5aSThomas Gleixner fixups = oldfixups = debug_objects_fixups; 12873ac7fe5aSThomas Gleixner warnings = oldwarnings = debug_objects_warnings; 12883ac7fe5aSThomas Gleixner descr_test = &descr_type_test; 12893ac7fe5aSThomas Gleixner 12903ac7fe5aSThomas Gleixner debug_object_init(&obj, &descr_type_test); 12913ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings)) 12923ac7fe5aSThomas Gleixner goto out; 12933ac7fe5aSThomas Gleixner debug_object_activate(&obj, &descr_type_test); 12943ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) 12953ac7fe5aSThomas Gleixner goto out; 12963ac7fe5aSThomas Gleixner debug_object_activate(&obj, &descr_type_test); 12973ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings)) 12983ac7fe5aSThomas Gleixner goto out; 12993ac7fe5aSThomas Gleixner debug_object_deactivate(&obj, &descr_type_test); 13003ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings)) 13013ac7fe5aSThomas Gleixner goto out; 13023ac7fe5aSThomas Gleixner debug_object_destroy(&obj, &descr_type_test); 13033ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings)) 13043ac7fe5aSThomas Gleixner goto out; 13053ac7fe5aSThomas Gleixner debug_object_init(&obj, &descr_type_test); 13063ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) 13073ac7fe5aSThomas Gleixner goto out; 13083ac7fe5aSThomas Gleixner debug_object_activate(&obj, &descr_type_test); 13093ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) 13103ac7fe5aSThomas Gleixner goto out; 13113ac7fe5aSThomas Gleixner debug_object_deactivate(&obj, &descr_type_test); 13123ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) 13133ac7fe5aSThomas Gleixner goto out; 13143ac7fe5aSThomas Gleixner debug_object_free(&obj, &descr_type_test); 13153ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings)) 13163ac7fe5aSThomas Gleixner goto out; 13173ac7fe5aSThomas Gleixner 13183ac7fe5aSThomas Gleixner obj.static_init = 1; 13193ac7fe5aSThomas Gleixner debug_object_activate(&obj, &descr_type_test); 13209f78ff00SStephen Boyd if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) 13213ac7fe5aSThomas Gleixner goto out; 13223ac7fe5aSThomas Gleixner debug_object_init(&obj, &descr_type_test); 13233ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings)) 13243ac7fe5aSThomas Gleixner goto out; 13253ac7fe5aSThomas Gleixner debug_object_free(&obj, &descr_type_test); 13263ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings)) 13273ac7fe5aSThomas Gleixner goto out; 13283ac7fe5aSThomas Gleixner 13293ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE 13303ac7fe5aSThomas Gleixner debug_object_init(&obj, &descr_type_test); 13313ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings)) 13323ac7fe5aSThomas Gleixner goto out; 13333ac7fe5aSThomas Gleixner debug_object_activate(&obj, &descr_type_test); 13343ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) 13353ac7fe5aSThomas Gleixner goto out; 13363ac7fe5aSThomas Gleixner __debug_check_no_obj_freed(&obj, sizeof(obj)); 13373ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings)) 13383ac7fe5aSThomas Gleixner goto out; 13393ac7fe5aSThomas Gleixner #endif 1340719e4843SFabian Frederick pr_info("selftest passed\n"); 13413ac7fe5aSThomas Gleixner 13423ac7fe5aSThomas Gleixner out: 13433ac7fe5aSThomas Gleixner debug_objects_fixups = oldfixups; 13443ac7fe5aSThomas Gleixner debug_objects_warnings = oldwarnings; 13453ac7fe5aSThomas Gleixner descr_test = NULL; 13463ac7fe5aSThomas Gleixner 13473ac7fe5aSThomas Gleixner local_irq_restore(flags); 1348661cc28bSThomas Gleixner return debug_objects_enabled; 13493ac7fe5aSThomas Gleixner } 13503ac7fe5aSThomas Gleixner #else 135155fb412eSThomas Gleixner static inline bool debug_objects_selftest(void) { return true; } 13523ac7fe5aSThomas Gleixner #endif 13533ac7fe5aSThomas Gleixner 13543ac7fe5aSThomas Gleixner /* 13553ac7fe5aSThomas Gleixner * Called during early boot to initialize the hash buckets and link 13563ac7fe5aSThomas Gleixner * the static object pool objects into the poll list. After this call 13573ac7fe5aSThomas Gleixner * the object tracker is fully operational. 13583ac7fe5aSThomas Gleixner */ 13593ac7fe5aSThomas Gleixner void __init debug_objects_early_init(void) 13603ac7fe5aSThomas Gleixner { 13613ac7fe5aSThomas Gleixner int i; 13623ac7fe5aSThomas Gleixner 13633ac7fe5aSThomas Gleixner for (i = 0; i < ODEBUG_HASH_SIZE; i++) 1364aef9cb05SThomas Gleixner raw_spin_lock_init(&obj_hash[i].lock); 13653ac7fe5aSThomas Gleixner 1366cb58d190SThomas Gleixner /* Keep early boot simple and add everything to the boot list */ 13673ac7fe5aSThomas Gleixner for (i = 0; i < ODEBUG_POOL_SIZE; i++) 1368cb58d190SThomas Gleixner hlist_add_head(&obj_static_pool[i].node, &pool_boot); 13693ac7fe5aSThomas Gleixner } 13703ac7fe5aSThomas Gleixner 13713ac7fe5aSThomas Gleixner /* 137255fb412eSThomas Gleixner * Convert the statically allocated objects to dynamic ones. 137355fb412eSThomas Gleixner * debug_objects_mem_init() is called early so only one CPU is up and 137455fb412eSThomas Gleixner * interrupts are disabled, which means it is safe to replace the active 137555fb412eSThomas Gleixner * object references. 13761be1cb7bSThomas Gleixner */ 137755fb412eSThomas Gleixner static bool __init debug_objects_replace_static_objects(struct kmem_cache *cache) 13781be1cb7bSThomas Gleixner { 13791be1cb7bSThomas Gleixner struct debug_bucket *db = obj_hash; 138055fb412eSThomas Gleixner struct hlist_node *tmp; 1381aebbfe07SThomas Gleixner struct debug_obj *obj; 13821be1cb7bSThomas Gleixner HLIST_HEAD(objects); 1383241463f4SThomas Gleixner int i; 13841be1cb7bSThomas Gleixner 1385aebbfe07SThomas Gleixner for (i = 0; i < ODEBUG_POOL_SIZE; i += ODEBUG_BATCH_SIZE) { 1386aebbfe07SThomas Gleixner if (!kmem_alloc_batch(&objects, cache, GFP_KERNEL)) 13871be1cb7bSThomas Gleixner goto free; 1388aebbfe07SThomas Gleixner pool_push_batch(&pool_global, &objects); 13891be1cb7bSThomas Gleixner } 13901be1cb7bSThomas Gleixner 1391aebbfe07SThomas Gleixner /* Disconnect the boot pool. */ 1392cb58d190SThomas Gleixner pool_boot.first = NULL; 13931be1cb7bSThomas Gleixner 13941be1cb7bSThomas Gleixner /* Replace the active object references */ 13951be1cb7bSThomas Gleixner for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) { 13961be1cb7bSThomas Gleixner hlist_move_list(&db->list, &objects); 13971be1cb7bSThomas Gleixner 1398b67bfe0dSSasha Levin hlist_for_each_entry(obj, &objects, node) { 1399aebbfe07SThomas Gleixner struct debug_obj *new = pcpu_alloc(); 1400aebbfe07SThomas Gleixner 14011be1cb7bSThomas Gleixner /* copy object data */ 14021be1cb7bSThomas Gleixner *new = *obj; 14031be1cb7bSThomas Gleixner hlist_add_head(&new->node, &db->list); 14041be1cb7bSThomas Gleixner } 14051be1cb7bSThomas Gleixner } 140655fb412eSThomas Gleixner return true; 14071be1cb7bSThomas Gleixner free: 140849a5cb82SThomas Gleixner /* Can't use free_object_list() as the cache is not populated yet */ 1409aebbfe07SThomas Gleixner hlist_for_each_entry_safe(obj, tmp, &pool_global.objects, node) { 14101be1cb7bSThomas Gleixner hlist_del(&obj->node); 141155fb412eSThomas Gleixner kmem_cache_free(cache, obj); 14121be1cb7bSThomas Gleixner } 141355fb412eSThomas Gleixner return false; 14141be1cb7bSThomas Gleixner } 14151be1cb7bSThomas Gleixner 14161be1cb7bSThomas Gleixner /* 14173ac7fe5aSThomas Gleixner * Called after the kmem_caches are functional to setup a dedicated 14183ac7fe5aSThomas Gleixner * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag 14193ac7fe5aSThomas Gleixner * prevents that the debug code is called on kmem_cache_free() for the 14203ac7fe5aSThomas Gleixner * debug tracker objects to avoid recursive calls. 14213ac7fe5aSThomas Gleixner */ 14223ac7fe5aSThomas Gleixner void __init debug_objects_mem_init(void) 14233ac7fe5aSThomas Gleixner { 142455fb412eSThomas Gleixner struct kmem_cache *cache; 14253f397bf9SThomas Gleixner int extras; 1426d86998b1SWaiman Long 14273ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 14283ac7fe5aSThomas Gleixner return; 14293ac7fe5aSThomas Gleixner 143055fb412eSThomas Gleixner if (!debug_objects_selftest()) 1431eabb7f1aSwuchi return; 143255fb412eSThomas Gleixner 143355fb412eSThomas Gleixner cache = kmem_cache_create("debug_objects_cache", sizeof (struct debug_obj), 0, 143455fb412eSThomas Gleixner SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE, NULL); 143555fb412eSThomas Gleixner 143655fb412eSThomas Gleixner if (!cache || !debug_objects_replace_static_objects(cache)) { 1437661cc28bSThomas Gleixner debug_objects_enabled = false; 143855fb412eSThomas Gleixner pr_warn("Out of memory.\n"); 143955fb412eSThomas Gleixner return; 144055fb412eSThomas Gleixner } 144155fb412eSThomas Gleixner 144255fb412eSThomas Gleixner /* 144355fb412eSThomas Gleixner * Adjust the thresholds for allocating and freeing objects 144455fb412eSThomas Gleixner * according to the number of possible CPUs available in the 144555fb412eSThomas Gleixner * system. 144655fb412eSThomas Gleixner */ 144755fb412eSThomas Gleixner extras = num_possible_cpus() * ODEBUG_BATCH_SIZE; 144896a9a042SThomas Gleixner pool_global.max_cnt += extras; 144996a9a042SThomas Gleixner pool_global.min_cnt += extras; 145055fb412eSThomas Gleixner 145155fb412eSThomas Gleixner /* Everything worked. Expose the cache */ 145255fb412eSThomas Gleixner obj_cache = cache; 145314077b9eSThomas Gleixner static_branch_enable(&obj_cache_enabled); 1454634d61f4SWaiman Long 145588451f2cSZqiang #ifdef CONFIG_HOTPLUG_CPU 145688451f2cSZqiang cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL, 145788451f2cSZqiang object_cpu_offline); 145888451f2cSZqiang #endif 145955fb412eSThomas Gleixner return; 14603ac7fe5aSThomas Gleixner } 1461