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 31a201a96bSThomas 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--; 258*13f9ca72SThomas Gleixner /* 259*13f9ca72SThomas Gleixner * If this emptied a batch try to refill from the 260*13f9ca72SThomas Gleixner * free pool. Don't do that if this was the top-most 261*13f9ca72SThomas Gleixner * batch as pcpu_free() expects the per CPU pool 262*13f9ca72SThomas Gleixner * to be less than ODEBUG_POOL_PERCPU_SIZE. 263*13f9ca72SThomas Gleixner */ 264*13f9ca72SThomas Gleixner if (unlikely(pcp->cnt < (ODEBUG_POOL_PERCPU_SIZE - ODEBUG_BATCH_SIZE) && 265*13f9ca72SThomas Gleixner !(pcp->cnt % ODEBUG_BATCH_SIZE))) { 266*13f9ca72SThomas Gleixner /* 267*13f9ca72SThomas Gleixner * Don't try to allocate from the regular pool here 268*13f9ca72SThomas Gleixner * to not exhaust it prematurely. 269*13f9ca72SThomas Gleixner */ 270*13f9ca72SThomas Gleixner if (pool_count(&pool_to_free)) { 271*13f9ca72SThomas Gleixner guard(raw_spinlock)(&pool_lock); 272*13f9ca72SThomas Gleixner pool_move_batch(pcp, &pool_to_free); 273*13f9ca72SThomas Gleixner pcpu_refill_stats(); 274*13f9ca72SThomas Gleixner } 275*13f9ca72SThomas Gleixner } 276fb60c004SThomas Gleixner return obj; 277fb60c004SThomas Gleixner } 278fb60c004SThomas Gleixner 279fb60c004SThomas Gleixner guard(raw_spinlock)(&pool_lock); 280fb60c004SThomas Gleixner if (!pool_move_batch(pcp, &pool_to_free)) { 281fb60c004SThomas Gleixner if (!pool_move_batch(pcp, &pool_global)) 282fb60c004SThomas Gleixner return NULL; 283fb60c004SThomas Gleixner } 2842638345dSThomas Gleixner pcpu_refill_stats(); 285fb60c004SThomas Gleixner } 286fb60c004SThomas Gleixner } 287fb60c004SThomas Gleixner 288a3b9e191SThomas Gleixner static void pcpu_free(struct debug_obj *obj) 289a3b9e191SThomas Gleixner { 290a3b9e191SThomas Gleixner struct obj_pool *pcp = this_cpu_ptr(&pool_pcpu); 291f57ebb92SThomas Gleixner struct debug_obj *first; 292a3b9e191SThomas Gleixner 293a3b9e191SThomas Gleixner lockdep_assert_irqs_disabled(); 294a3b9e191SThomas Gleixner 295f57ebb92SThomas Gleixner if (!(pcp->cnt % ODEBUG_BATCH_SIZE)) { 296f57ebb92SThomas Gleixner obj->batch_last = &obj->node; 297f57ebb92SThomas Gleixner } else { 298f57ebb92SThomas Gleixner first = hlist_entry(pcp->objects.first, typeof(*first), node); 299f57ebb92SThomas Gleixner obj->batch_last = first->batch_last; 300f57ebb92SThomas Gleixner } 301a3b9e191SThomas Gleixner hlist_add_head(&obj->node, &pcp->objects); 302a3b9e191SThomas Gleixner pcp->cnt++; 303a3b9e191SThomas Gleixner 304a3b9e191SThomas Gleixner /* Pool full ? */ 305a3b9e191SThomas Gleixner if (pcp->cnt < ODEBUG_POOL_PERCPU_SIZE) 306a3b9e191SThomas Gleixner return; 307a3b9e191SThomas Gleixner 308a3b9e191SThomas Gleixner /* Remove a batch from the per CPU pool */ 309a3b9e191SThomas Gleixner guard(raw_spinlock)(&pool_lock); 310a3b9e191SThomas Gleixner /* Try to fit the batch into the pool_global first */ 311a3b9e191SThomas Gleixner if (!pool_move_batch(&pool_global, pcp)) 312a3b9e191SThomas Gleixner pool_move_batch(&pool_to_free, pcp); 3132638345dSThomas Gleixner WRITE_ONCE(pool_global.stats.cur_used, pool_global.stats.cur_used - ODEBUG_BATCH_SIZE); 314a3b9e191SThomas Gleixner } 315a3b9e191SThomas Gleixner 31649a5cb82SThomas Gleixner static void free_object_list(struct hlist_head *head) 31749a5cb82SThomas Gleixner { 31849a5cb82SThomas Gleixner struct hlist_node *tmp; 31949a5cb82SThomas Gleixner struct debug_obj *obj; 32049a5cb82SThomas Gleixner int cnt = 0; 32149a5cb82SThomas Gleixner 32249a5cb82SThomas Gleixner hlist_for_each_entry_safe(obj, tmp, head, node) { 32349a5cb82SThomas Gleixner hlist_del(&obj->node); 32449a5cb82SThomas Gleixner kmem_cache_free(obj_cache, obj); 32549a5cb82SThomas Gleixner cnt++; 32649a5cb82SThomas Gleixner } 32749a5cb82SThomas Gleixner debug_objects_freed += cnt; 32849a5cb82SThomas Gleixner } 32949a5cb82SThomas Gleixner 330d8c6cd3aSZhen Lei static void fill_pool_from_freelist(void) 3313ac7fe5aSThomas Gleixner { 332d8c6cd3aSZhen Lei static unsigned long state; 3333ac7fe5aSThomas Gleixner 33436c4ead6SYang Shi /* 33563a4a9b5SZhen Lei * Reuse objs from the global obj_to_free list; they will be 33663a4a9b5SZhen Lei * reinitialized when allocating. 33736c4ead6SYang Shi */ 338e18328ffSThomas Gleixner if (!pool_count(&pool_to_free)) 339d8c6cd3aSZhen Lei return; 340d8c6cd3aSZhen Lei 341d8c6cd3aSZhen Lei /* 342d8c6cd3aSZhen Lei * Prevent the context from being scheduled or interrupted after 343d8c6cd3aSZhen Lei * setting the state flag; 344d8c6cd3aSZhen Lei */ 345d8c6cd3aSZhen Lei guard(irqsave)(); 346d8c6cd3aSZhen Lei 347d8c6cd3aSZhen Lei /* 348d8c6cd3aSZhen Lei * Avoid lock contention on &pool_lock and avoid making the cache 349d8c6cd3aSZhen Lei * line exclusive by testing the bit before attempting to set it. 350d8c6cd3aSZhen Lei */ 351d8c6cd3aSZhen Lei if (test_bit(0, &state) || test_and_set_bit(0, &state)) 352d8c6cd3aSZhen Lei return; 353d8c6cd3aSZhen Lei 354fb60c004SThomas Gleixner /* Avoid taking the lock when there is no work to do */ 355fb60c004SThomas Gleixner while (pool_should_refill(&pool_global) && pool_count(&pool_to_free)) { 356d8c6cd3aSZhen Lei guard(raw_spinlock)(&pool_lock); 357fb60c004SThomas Gleixner /* Move a batch if possible */ 358fb60c004SThomas Gleixner pool_move_batch(&pool_global, &pool_to_free); 35936c4ead6SYang Shi } 360d8c6cd3aSZhen Lei clear_bit(0, &state); 36136c4ead6SYang Shi } 36236c4ead6SYang Shi 363aebbfe07SThomas Gleixner static bool kmem_alloc_batch(struct hlist_head *head, struct kmem_cache *cache, gfp_t gfp) 364aebbfe07SThomas Gleixner { 365aebbfe07SThomas Gleixner struct hlist_node *last = NULL; 366aebbfe07SThomas Gleixner struct debug_obj *obj; 367aebbfe07SThomas Gleixner 368aebbfe07SThomas Gleixner for (int cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) { 369aebbfe07SThomas Gleixner obj = kmem_cache_zalloc(cache, gfp); 370aebbfe07SThomas Gleixner if (!obj) { 371aebbfe07SThomas Gleixner free_object_list(head); 372aebbfe07SThomas Gleixner return false; 373aebbfe07SThomas Gleixner } 374aebbfe07SThomas Gleixner debug_objects_allocated++; 375aebbfe07SThomas Gleixner 376aebbfe07SThomas Gleixner if (!last) 377aebbfe07SThomas Gleixner last = &obj->node; 378aebbfe07SThomas Gleixner obj->batch_last = last; 379aebbfe07SThomas Gleixner 380aebbfe07SThomas Gleixner hlist_add_head(&obj->node, head); 381aebbfe07SThomas Gleixner } 382aebbfe07SThomas Gleixner return true; 383aebbfe07SThomas Gleixner } 384aebbfe07SThomas Gleixner 385d8c6cd3aSZhen Lei static void fill_pool(void) 386d8c6cd3aSZhen Lei { 387d8c6cd3aSZhen Lei static atomic_t cpus_allocating; 388d8c6cd3aSZhen Lei 389d8c6cd3aSZhen Lei /* 390d8c6cd3aSZhen Lei * Avoid allocation and lock contention when: 391d8c6cd3aSZhen Lei * - One other CPU is already allocating 392d8c6cd3aSZhen Lei * - the global pool has not reached the critical level yet 393d8c6cd3aSZhen Lei */ 39496a9a042SThomas Gleixner if (!pool_must_refill(&pool_global) && atomic_read(&cpus_allocating)) 3951fda107dSThomas Gleixner return; 3963ac7fe5aSThomas Gleixner 397d8c6cd3aSZhen Lei atomic_inc(&cpus_allocating); 39896a9a042SThomas Gleixner while (pool_should_refill(&pool_global)) { 399813fd078SZhen Lei HLIST_HEAD(head); 4003ac7fe5aSThomas Gleixner 401aebbfe07SThomas Gleixner if (!kmem_alloc_batch(&head, obj_cache, __GFP_HIGH | __GFP_NOWARN)) 402d8c6cd3aSZhen Lei break; 4033ac7fe5aSThomas Gleixner 404d8c6cd3aSZhen Lei guard(raw_spinlock_irqsave)(&pool_lock); 405aebbfe07SThomas Gleixner if (!pool_push_batch(&pool_global, &head)) 406aebbfe07SThomas Gleixner pool_push_batch(&pool_to_free, &head); 4073ac7fe5aSThomas Gleixner } 408d8c6cd3aSZhen Lei atomic_dec(&cpus_allocating); 4093ac7fe5aSThomas Gleixner } 4103ac7fe5aSThomas Gleixner 4113ac7fe5aSThomas Gleixner /* 4123ac7fe5aSThomas Gleixner * Lookup an object in the hash bucket. 4133ac7fe5aSThomas Gleixner */ 4143ac7fe5aSThomas Gleixner static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b) 4153ac7fe5aSThomas Gleixner { 4163ac7fe5aSThomas Gleixner struct debug_obj *obj; 4173ac7fe5aSThomas Gleixner int cnt = 0; 4183ac7fe5aSThomas Gleixner 419b67bfe0dSSasha Levin hlist_for_each_entry(obj, &b->list, node) { 4203ac7fe5aSThomas Gleixner cnt++; 4213ac7fe5aSThomas Gleixner if (obj->object == addr) 4223ac7fe5aSThomas Gleixner return obj; 4233ac7fe5aSThomas Gleixner } 4243ac7fe5aSThomas Gleixner if (cnt > debug_objects_maxchain) 4253ac7fe5aSThomas Gleixner debug_objects_maxchain = cnt; 4263ac7fe5aSThomas Gleixner 4273ac7fe5aSThomas Gleixner return NULL; 4283ac7fe5aSThomas Gleixner } 4293ac7fe5aSThomas Gleixner 430fb60c004SThomas Gleixner static struct debug_obj *alloc_object(void *addr, struct debug_bucket *b, 431fb60c004SThomas Gleixner const struct debug_obj_descr *descr) 432d86998b1SWaiman Long { 433d86998b1SWaiman Long struct debug_obj *obj; 434d86998b1SWaiman Long 43514077b9eSThomas Gleixner if (static_branch_likely(&obj_cache_enabled)) 436fb60c004SThomas Gleixner obj = pcpu_alloc(); 437fb60c004SThomas Gleixner else 438cb58d190SThomas Gleixner obj = __alloc_object(&pool_boot); 4393ac7fe5aSThomas Gleixner 440fb60c004SThomas Gleixner if (likely(obj)) { 441d86998b1SWaiman Long obj->object = addr; 442d86998b1SWaiman Long obj->descr = descr; 443d86998b1SWaiman Long obj->state = ODEBUG_STATE_NONE; 444d86998b1SWaiman Long obj->astate = 0; 445d86998b1SWaiman Long hlist_add_head(&obj->node, &b->list); 446d86998b1SWaiman Long } 4473ac7fe5aSThomas Gleixner return obj; 4483ac7fe5aSThomas Gleixner } 4493ac7fe5aSThomas Gleixner 4509ce99c6dSThomas Gleixner /* workqueue function to free objects. */ 451337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work) 452337fff8bSThomas Gleixner { 4539ce99c6dSThomas Gleixner bool free = true; 454337fff8bSThomas Gleixner 455a7344a68SWaiman Long WRITE_ONCE(obj_freeing, false); 4569ce99c6dSThomas Gleixner 4579ce99c6dSThomas Gleixner if (!pool_count(&pool_to_free)) 458858274b6SWaiman Long return; 45936c4ead6SYang Shi 4609ce99c6dSThomas Gleixner for (unsigned int cnt = 0; cnt < ODEBUG_FREE_WORK_MAX; cnt++) { 4619ce99c6dSThomas Gleixner HLIST_HEAD(tofree); 462a7344a68SWaiman Long 4639ce99c6dSThomas Gleixner /* Acquire and drop the lock for each batch */ 4649ce99c6dSThomas Gleixner scoped_guard(raw_spinlock_irqsave, &pool_lock) { 4659ce99c6dSThomas Gleixner if (!pool_to_free.cnt) 466a7344a68SWaiman Long return; 46736c4ead6SYang Shi 4689ce99c6dSThomas Gleixner /* Refill the global pool if possible */ 4699ce99c6dSThomas Gleixner if (pool_move_batch(&pool_global, &pool_to_free)) { 4709ce99c6dSThomas Gleixner /* Don't free as there seems to be demand */ 4719ce99c6dSThomas Gleixner free = false; 4729ce99c6dSThomas Gleixner } else if (free) { 4739ce99c6dSThomas Gleixner pool_pop_batch(&tofree, &pool_to_free); 4749ce99c6dSThomas Gleixner } else { 4759ce99c6dSThomas Gleixner return; 47636c4ead6SYang Shi } 4779ce99c6dSThomas Gleixner } 47849a5cb82SThomas Gleixner free_object_list(&tofree); 479337fff8bSThomas Gleixner } 4809ce99c6dSThomas Gleixner } 481337fff8bSThomas Gleixner 482a7344a68SWaiman Long static void __free_object(struct debug_obj *obj) 483636e1970SYang Shi { 484cb58d190SThomas Gleixner guard(irqsave)(); 48514077b9eSThomas Gleixner if (static_branch_likely(&obj_cache_enabled)) 486a3b9e191SThomas Gleixner pcpu_free(obj); 487a3b9e191SThomas Gleixner else 488cb58d190SThomas Gleixner hlist_add_head(&obj->node, &pool_boot); 489636e1970SYang Shi } 490636e1970SYang Shi 491337fff8bSThomas Gleixner /* 492337fff8bSThomas Gleixner * Put the object back into the pool and schedule work to free objects 493337fff8bSThomas Gleixner * if necessary. 4943ac7fe5aSThomas Gleixner */ 4953ac7fe5aSThomas Gleixner static void free_object(struct debug_obj *obj) 4963ac7fe5aSThomas Gleixner { 497a7344a68SWaiman Long __free_object(obj); 498e18328ffSThomas Gleixner if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) { 499a7344a68SWaiman Long WRITE_ONCE(obj_freeing, true); 500a7344a68SWaiman Long schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY); 501a7344a68SWaiman Long } 5023ac7fe5aSThomas Gleixner } 5033ac7fe5aSThomas Gleixner 504a2a70238SThomas Gleixner static void put_objects(struct hlist_head *list) 50588451f2cSZqiang { 50688451f2cSZqiang struct hlist_node *tmp; 50788451f2cSZqiang struct debug_obj *obj; 50888451f2cSZqiang 509a2a70238SThomas Gleixner /* 510a2a70238SThomas Gleixner * Using free_object() puts the objects into reuse or schedules 511a2a70238SThomas Gleixner * them for freeing and it get's all the accounting correct. 512a2a70238SThomas Gleixner */ 513a2a70238SThomas Gleixner hlist_for_each_entry_safe(obj, tmp, list, node) { 51488451f2cSZqiang hlist_del(&obj->node); 515a2a70238SThomas Gleixner free_object(obj); 516a2a70238SThomas Gleixner } 51788451f2cSZqiang } 518eabb7f1aSwuchi 51949968cf1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU 520a2a70238SThomas Gleixner static int object_cpu_offline(unsigned int cpu) 521a2a70238SThomas Gleixner { 522a2a70238SThomas Gleixner /* Remote access is safe as the CPU is dead already */ 52318b8afcbSThomas Gleixner struct obj_pool *pcp = per_cpu_ptr(&pool_pcpu, cpu); 524eabb7f1aSwuchi 52518b8afcbSThomas Gleixner put_objects(&pcp->objects); 52618b8afcbSThomas Gleixner pcp->cnt = 0; 52788451f2cSZqiang return 0; 52888451f2cSZqiang } 52988451f2cSZqiang #endif 53088451f2cSZqiang 53149968cf1SThomas Gleixner /* Out of memory. Free all objects from hash */ 5323ac7fe5aSThomas Gleixner static void debug_objects_oom(void) 5333ac7fe5aSThomas Gleixner { 5343ac7fe5aSThomas Gleixner struct debug_bucket *db = obj_hash; 535673d62ccSVegard Nossum HLIST_HEAD(freelist); 5363ac7fe5aSThomas Gleixner 537719e4843SFabian Frederick pr_warn("Out of memory. ODEBUG disabled\n"); 5383ac7fe5aSThomas Gleixner 53949968cf1SThomas Gleixner for (int i = 0; i < ODEBUG_HASH_SIZE; i++, db++) { 54049968cf1SThomas Gleixner scoped_guard(raw_spinlock_irqsave, &db->lock) 541673d62ccSVegard Nossum hlist_move_list(&db->list, &freelist); 542673d62ccSVegard Nossum 54349968cf1SThomas Gleixner put_objects(&freelist); 5443ac7fe5aSThomas Gleixner } 5453ac7fe5aSThomas Gleixner } 5463ac7fe5aSThomas Gleixner 5473ac7fe5aSThomas Gleixner /* 5483ac7fe5aSThomas Gleixner * We use the pfn of the address for the hash. That way we can check 5493ac7fe5aSThomas Gleixner * for freed objects simply by checking the affected bucket. 5503ac7fe5aSThomas Gleixner */ 5513ac7fe5aSThomas Gleixner static struct debug_bucket *get_bucket(unsigned long addr) 5523ac7fe5aSThomas Gleixner { 5533ac7fe5aSThomas Gleixner unsigned long hash; 5543ac7fe5aSThomas Gleixner 5553ac7fe5aSThomas Gleixner hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS); 5563ac7fe5aSThomas Gleixner return &obj_hash[hash]; 5573ac7fe5aSThomas Gleixner } 5583ac7fe5aSThomas Gleixner 5593ac7fe5aSThomas Gleixner static void debug_print_object(struct debug_obj *obj, char *msg) 5603ac7fe5aSThomas Gleixner { 561aedcade6SStephen Boyd const struct debug_obj_descr *descr = obj->descr; 5623ac7fe5aSThomas Gleixner static int limit; 5633ac7fe5aSThomas Gleixner 5648b64d420STetsuo Handa /* 5658b64d420STetsuo Handa * Don't report if lookup_object_or_alloc() by the current thread 5668b64d420STetsuo Handa * failed because lookup_object_or_alloc()/debug_objects_oom() by a 5678b64d420STetsuo Handa * concurrent thread turned off debug_objects_enabled and cleared 5688b64d420STetsuo Handa * the hash buckets. 5698b64d420STetsuo Handa */ 5708b64d420STetsuo Handa if (!debug_objects_enabled) 5718b64d420STetsuo Handa return; 5728b64d420STetsuo Handa 57399777288SStanislaw Gruszka if (limit < 5 && descr != descr_test) { 57499777288SStanislaw Gruszka void *hint = descr->debug_hint ? 57599777288SStanislaw Gruszka descr->debug_hint(obj->object) : NULL; 5763ac7fe5aSThomas Gleixner limit++; 577a5d8e467SMathieu Desnoyers WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) " 578c4db2d3bSStephen Boyd "object: %p object type: %s hint: %pS\n", 579a5d8e467SMathieu Desnoyers msg, obj_states[obj->state], obj->astate, 580c4db2d3bSStephen Boyd obj->object, descr->name, hint); 5813ac7fe5aSThomas Gleixner } 5823ac7fe5aSThomas Gleixner debug_objects_warnings++; 5833ac7fe5aSThomas Gleixner } 5843ac7fe5aSThomas Gleixner 5853ac7fe5aSThomas Gleixner /* 5863ac7fe5aSThomas Gleixner * Try to repair the damage, so we have a better chance to get useful 5873ac7fe5aSThomas Gleixner * debug output. 5883ac7fe5aSThomas Gleixner */ 589b1e4d9d8SDu, Changbin static bool 590b1e4d9d8SDu, Changbin debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state), 5913ac7fe5aSThomas Gleixner void * addr, enum debug_obj_state state) 5923ac7fe5aSThomas Gleixner { 593b1e4d9d8SDu, Changbin if (fixup && fixup(addr, state)) { 594b1e4d9d8SDu, Changbin debug_objects_fixups++; 595b1e4d9d8SDu, Changbin return true; 596b1e4d9d8SDu, Changbin } 597b1e4d9d8SDu, Changbin return false; 5983ac7fe5aSThomas Gleixner } 5993ac7fe5aSThomas Gleixner 6003ac7fe5aSThomas Gleixner static void debug_object_is_on_stack(void *addr, int onstack) 6013ac7fe5aSThomas Gleixner { 6023ac7fe5aSThomas Gleixner int is_on_stack; 6033ac7fe5aSThomas Gleixner static int limit; 6043ac7fe5aSThomas Gleixner 6053ac7fe5aSThomas Gleixner if (limit > 4) 6063ac7fe5aSThomas Gleixner return; 6073ac7fe5aSThomas Gleixner 6088b05c7e6SFUJITA Tomonori is_on_stack = object_is_on_stack(addr); 6093ac7fe5aSThomas Gleixner if (is_on_stack == onstack) 6103ac7fe5aSThomas Gleixner return; 6113ac7fe5aSThomas Gleixner 6123ac7fe5aSThomas Gleixner limit++; 6133ac7fe5aSThomas Gleixner if (is_on_stack) 614fc91a3c4SJoel Fernandes (Google) pr_warn("object %p is on stack %p, but NOT annotated.\n", addr, 615fc91a3c4SJoel Fernandes (Google) task_stack_page(current)); 6163ac7fe5aSThomas Gleixner else 617fc91a3c4SJoel Fernandes (Google) pr_warn("object %p is NOT on stack %p, but annotated.\n", addr, 618fc91a3c4SJoel Fernandes (Google) task_stack_page(current)); 619fc91a3c4SJoel Fernandes (Google) 6203ac7fe5aSThomas Gleixner WARN_ON(1); 6213ac7fe5aSThomas Gleixner } 6223ac7fe5aSThomas Gleixner 62363a75969SThomas Gleixner static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b, 62463a75969SThomas Gleixner const struct debug_obj_descr *descr, 62563a75969SThomas Gleixner bool onstack, bool alloc_ifstatic) 62663a75969SThomas Gleixner { 62763a75969SThomas Gleixner struct debug_obj *obj = lookup_object(addr, b); 62863a75969SThomas Gleixner enum debug_obj_state state = ODEBUG_STATE_NONE; 62963a75969SThomas Gleixner 63063a75969SThomas Gleixner if (likely(obj)) 63163a75969SThomas Gleixner return obj; 63263a75969SThomas Gleixner 63363a75969SThomas Gleixner /* 63463a75969SThomas Gleixner * debug_object_init() unconditionally allocates untracked 63563a75969SThomas Gleixner * objects. It does not matter whether it is a static object or 63663a75969SThomas Gleixner * not. 63763a75969SThomas Gleixner * 63863a75969SThomas Gleixner * debug_object_assert_init() and debug_object_activate() allow 63963a75969SThomas Gleixner * allocation only if the descriptor callback confirms that the 64063a75969SThomas Gleixner * object is static and considered initialized. For non-static 64163a75969SThomas Gleixner * objects the allocation needs to be done from the fixup callback. 64263a75969SThomas Gleixner */ 64363a75969SThomas Gleixner if (unlikely(alloc_ifstatic)) { 64463a75969SThomas Gleixner if (!descr->is_static_object || !descr->is_static_object(addr)) 64563a75969SThomas Gleixner return ERR_PTR(-ENOENT); 64663a75969SThomas Gleixner /* Statically allocated objects are considered initialized */ 64763a75969SThomas Gleixner state = ODEBUG_STATE_INIT; 64863a75969SThomas Gleixner } 64963a75969SThomas Gleixner 65063a75969SThomas Gleixner obj = alloc_object(addr, b, descr); 65163a75969SThomas Gleixner if (likely(obj)) { 65263a75969SThomas Gleixner obj->state = state; 65363a75969SThomas Gleixner debug_object_is_on_stack(addr, onstack); 65463a75969SThomas Gleixner return obj; 65563a75969SThomas Gleixner } 65663a75969SThomas Gleixner 65763a75969SThomas Gleixner /* Out of memory. Do the cleanup outside of the locked region */ 658661cc28bSThomas Gleixner debug_objects_enabled = false; 65963a75969SThomas Gleixner return NULL; 66063a75969SThomas Gleixner } 66163a75969SThomas Gleixner 6620af462f1SThomas Gleixner static void debug_objects_fill_pool(void) 6630af462f1SThomas Gleixner { 66414077b9eSThomas Gleixner if (!static_branch_likely(&obj_cache_enabled)) 665d8c6cd3aSZhen Lei return; 666d8c6cd3aSZhen Lei 66796a9a042SThomas Gleixner if (likely(!pool_should_refill(&pool_global))) 668d8c6cd3aSZhen Lei return; 669d8c6cd3aSZhen Lei 670d8c6cd3aSZhen Lei /* Try reusing objects from obj_to_free_list */ 671d8c6cd3aSZhen Lei fill_pool_from_freelist(); 672d8c6cd3aSZhen Lei 67396a9a042SThomas Gleixner if (likely(!pool_should_refill(&pool_global))) 674d8c6cd3aSZhen Lei return; 675d8c6cd3aSZhen Lei 6760af462f1SThomas Gleixner /* 6770af462f1SThomas Gleixner * On RT enabled kernels the pool refill must happen in preemptible 6780cce06baSPeter Zijlstra * context -- for !RT kernels we rely on the fact that spinlock_t and 6790cce06baSPeter Zijlstra * raw_spinlock_t are basically the same type and this lock-type 6800cce06baSPeter Zijlstra * inversion works just fine. 6810af462f1SThomas Gleixner */ 6820cce06baSPeter Zijlstra if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible()) { 6830cce06baSPeter Zijlstra /* 6840cce06baSPeter Zijlstra * Annotate away the spinlock_t inside raw_spinlock_t warning 6850cce06baSPeter Zijlstra * by temporarily raising the wait-type to WAIT_SLEEP, matching 6860cce06baSPeter Zijlstra * the preemptible() condition above. 6870cce06baSPeter Zijlstra */ 6880cce06baSPeter Zijlstra static DEFINE_WAIT_OVERRIDE_MAP(fill_pool_map, LD_WAIT_SLEEP); 6890cce06baSPeter Zijlstra lock_map_acquire_try(&fill_pool_map); 6900af462f1SThomas Gleixner fill_pool(); 6910cce06baSPeter Zijlstra lock_map_release(&fill_pool_map); 6920cce06baSPeter Zijlstra } 6930af462f1SThomas Gleixner } 6940af462f1SThomas Gleixner 6953ac7fe5aSThomas Gleixner static void 696aedcade6SStephen Boyd __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack) 6973ac7fe5aSThomas Gleixner { 6989bb63626SAndrzej Hajda struct debug_obj *obj, o; 6993ac7fe5aSThomas Gleixner struct debug_bucket *db; 7003ac7fe5aSThomas Gleixner unsigned long flags; 7013ac7fe5aSThomas Gleixner 7020af462f1SThomas Gleixner debug_objects_fill_pool(); 70350db04ddSVegard Nossum 7043ac7fe5aSThomas Gleixner db = get_bucket((unsigned long) addr); 7053ac7fe5aSThomas Gleixner 706aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 7073ac7fe5aSThomas Gleixner 70863a75969SThomas Gleixner obj = lookup_object_or_alloc(addr, db, descr, onstack, false); 70963a75969SThomas Gleixner if (unlikely(!obj)) { 710aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 7113ac7fe5aSThomas Gleixner debug_objects_oom(); 7123ac7fe5aSThomas Gleixner return; 7133ac7fe5aSThomas Gleixner } 7143ac7fe5aSThomas Gleixner 7153ac7fe5aSThomas Gleixner switch (obj->state) { 7163ac7fe5aSThomas Gleixner case ODEBUG_STATE_NONE: 7173ac7fe5aSThomas Gleixner case ODEBUG_STATE_INIT: 7183ac7fe5aSThomas Gleixner case ODEBUG_STATE_INACTIVE: 7193ac7fe5aSThomas Gleixner obj->state = ODEBUG_STATE_INIT; 720aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 721d5f34153SWaiman Long return; 7223ac7fe5aSThomas Gleixner default: 7233ac7fe5aSThomas Gleixner break; 7243ac7fe5aSThomas Gleixner } 7253ac7fe5aSThomas Gleixner 7269bb63626SAndrzej Hajda o = *obj; 727aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 7289bb63626SAndrzej Hajda debug_print_object(&o, "init"); 7299bb63626SAndrzej Hajda 7309bb63626SAndrzej Hajda if (o.state == ODEBUG_STATE_ACTIVE) 7319bb63626SAndrzej Hajda debug_object_fixup(descr->fixup_init, addr, o.state); 7323ac7fe5aSThomas Gleixner } 7333ac7fe5aSThomas Gleixner 7343ac7fe5aSThomas Gleixner /** 7353ac7fe5aSThomas Gleixner * debug_object_init - debug checks when an object is initialized 7363ac7fe5aSThomas Gleixner * @addr: address of the object 7373ac7fe5aSThomas Gleixner * @descr: pointer to an object specific debug description structure 7383ac7fe5aSThomas Gleixner */ 739aedcade6SStephen Boyd void debug_object_init(void *addr, const struct debug_obj_descr *descr) 7403ac7fe5aSThomas Gleixner { 7413ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 7423ac7fe5aSThomas Gleixner return; 7433ac7fe5aSThomas Gleixner 7443ac7fe5aSThomas Gleixner __debug_object_init(addr, descr, 0); 7453ac7fe5aSThomas Gleixner } 746f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init); 7473ac7fe5aSThomas Gleixner 7483ac7fe5aSThomas Gleixner /** 7493ac7fe5aSThomas Gleixner * debug_object_init_on_stack - debug checks when an object on stack is 7503ac7fe5aSThomas Gleixner * initialized 7513ac7fe5aSThomas Gleixner * @addr: address of the object 7523ac7fe5aSThomas Gleixner * @descr: pointer to an object specific debug description structure 7533ac7fe5aSThomas Gleixner */ 754aedcade6SStephen Boyd void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr) 7553ac7fe5aSThomas Gleixner { 7563ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 7573ac7fe5aSThomas Gleixner return; 7583ac7fe5aSThomas Gleixner 7593ac7fe5aSThomas Gleixner __debug_object_init(addr, descr, 1); 7603ac7fe5aSThomas Gleixner } 761f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init_on_stack); 7623ac7fe5aSThomas Gleixner 7633ac7fe5aSThomas Gleixner /** 7643ac7fe5aSThomas Gleixner * debug_object_activate - debug checks when an object is activated 7653ac7fe5aSThomas Gleixner * @addr: address of the object 7663ac7fe5aSThomas Gleixner * @descr: pointer to an object specific debug description structure 767b778ae25SPaul E. McKenney * Returns 0 for success, -EINVAL for check failed. 7683ac7fe5aSThomas Gleixner */ 769aedcade6SStephen Boyd int debug_object_activate(void *addr, const struct debug_obj_descr *descr) 7703ac7fe5aSThomas Gleixner { 77163a75969SThomas Gleixner struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr }; 7723ac7fe5aSThomas Gleixner struct debug_bucket *db; 7733ac7fe5aSThomas Gleixner struct debug_obj *obj; 7743ac7fe5aSThomas Gleixner unsigned long flags; 7753ac7fe5aSThomas Gleixner 7763ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 777b778ae25SPaul E. McKenney return 0; 7783ac7fe5aSThomas Gleixner 7790af462f1SThomas Gleixner debug_objects_fill_pool(); 7800af462f1SThomas Gleixner 7813ac7fe5aSThomas Gleixner db = get_bucket((unsigned long) addr); 7823ac7fe5aSThomas Gleixner 783aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 7843ac7fe5aSThomas Gleixner 78563a75969SThomas Gleixner obj = lookup_object_or_alloc(addr, db, descr, false, true); 7869bb63626SAndrzej Hajda if (unlikely(!obj)) { 7879bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 7889bb63626SAndrzej Hajda debug_objects_oom(); 7899bb63626SAndrzej Hajda return 0; 7909bb63626SAndrzej Hajda } else if (likely(!IS_ERR(obj))) { 7913ac7fe5aSThomas Gleixner switch (obj->state) { 7929bb63626SAndrzej Hajda case ODEBUG_STATE_ACTIVE: 7939bb63626SAndrzej Hajda case ODEBUG_STATE_DESTROYED: 7949bb63626SAndrzej Hajda o = *obj; 7959bb63626SAndrzej Hajda break; 7963ac7fe5aSThomas Gleixner case ODEBUG_STATE_INIT: 7973ac7fe5aSThomas Gleixner case ODEBUG_STATE_INACTIVE: 7983ac7fe5aSThomas Gleixner obj->state = ODEBUG_STATE_ACTIVE; 7999bb63626SAndrzej Hajda fallthrough; 8003ac7fe5aSThomas Gleixner default: 801aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 802b778ae25SPaul E. McKenney return 0; 8033ac7fe5aSThomas Gleixner } 8049bb63626SAndrzej Hajda } 80563a75969SThomas Gleixner 8069bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 80763a75969SThomas Gleixner debug_print_object(&o, "activate"); 8089bb63626SAndrzej Hajda 8099bb63626SAndrzej Hajda switch (o.state) { 8109bb63626SAndrzej Hajda case ODEBUG_STATE_ACTIVE: 8119bb63626SAndrzej Hajda case ODEBUG_STATE_NOTAVAILABLE: 8129bb63626SAndrzej Hajda if (debug_object_fixup(descr->fixup_activate, addr, o.state)) 8139bb63626SAndrzej Hajda return 0; 8149bb63626SAndrzej Hajda fallthrough; 8159bb63626SAndrzej Hajda default: 8169bb63626SAndrzej Hajda return -EINVAL; 8179bb63626SAndrzej Hajda } 81863a75969SThomas Gleixner } 819f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_activate); 8203ac7fe5aSThomas Gleixner 8213ac7fe5aSThomas Gleixner /** 8223ac7fe5aSThomas Gleixner * debug_object_deactivate - debug checks when an object is deactivated 8233ac7fe5aSThomas Gleixner * @addr: address of the object 8243ac7fe5aSThomas Gleixner * @descr: pointer to an object specific debug description structure 8253ac7fe5aSThomas Gleixner */ 826aedcade6SStephen Boyd void debug_object_deactivate(void *addr, const struct debug_obj_descr *descr) 8273ac7fe5aSThomas Gleixner { 8289bb63626SAndrzej Hajda struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr }; 8293ac7fe5aSThomas Gleixner struct debug_bucket *db; 8303ac7fe5aSThomas Gleixner struct debug_obj *obj; 8313ac7fe5aSThomas Gleixner unsigned long flags; 8323ac7fe5aSThomas Gleixner 8333ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 8343ac7fe5aSThomas Gleixner return; 8353ac7fe5aSThomas Gleixner 8363ac7fe5aSThomas Gleixner db = get_bucket((unsigned long) addr); 8373ac7fe5aSThomas Gleixner 838aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 8393ac7fe5aSThomas Gleixner 8403ac7fe5aSThomas Gleixner obj = lookup_object(addr, db); 8413ac7fe5aSThomas Gleixner if (obj) { 8423ac7fe5aSThomas Gleixner switch (obj->state) { 8439bb63626SAndrzej Hajda case ODEBUG_STATE_DESTROYED: 8449bb63626SAndrzej Hajda break; 8453ac7fe5aSThomas Gleixner case ODEBUG_STATE_INIT: 8463ac7fe5aSThomas Gleixner case ODEBUG_STATE_INACTIVE: 8473ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 8489bb63626SAndrzej Hajda if (obj->astate) 8499bb63626SAndrzej Hajda break; 8503ac7fe5aSThomas Gleixner obj->state = ODEBUG_STATE_INACTIVE; 8519bb63626SAndrzej Hajda fallthrough; 8523ac7fe5aSThomas Gleixner default: 8539bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 8549bb63626SAndrzej Hajda return; 8553ac7fe5aSThomas Gleixner } 8569bb63626SAndrzej Hajda o = *obj; 857d5f34153SWaiman Long } 858d5f34153SWaiman Long 859d5f34153SWaiman Long raw_spin_unlock_irqrestore(&db->lock, flags); 8603ac7fe5aSThomas Gleixner debug_print_object(&o, "deactivate"); 8613ac7fe5aSThomas Gleixner } 862f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_deactivate); 8633ac7fe5aSThomas Gleixner 8643ac7fe5aSThomas Gleixner /** 8653ac7fe5aSThomas Gleixner * debug_object_destroy - debug checks when an object is destroyed 8663ac7fe5aSThomas Gleixner * @addr: address of the object 8673ac7fe5aSThomas Gleixner * @descr: pointer to an object specific debug description structure 8683ac7fe5aSThomas Gleixner */ 869aedcade6SStephen Boyd void debug_object_destroy(void *addr, const struct debug_obj_descr *descr) 8703ac7fe5aSThomas Gleixner { 8719bb63626SAndrzej Hajda struct debug_obj *obj, o; 8723ac7fe5aSThomas Gleixner struct debug_bucket *db; 8733ac7fe5aSThomas Gleixner unsigned long flags; 8743ac7fe5aSThomas Gleixner 8753ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 8763ac7fe5aSThomas Gleixner return; 8773ac7fe5aSThomas Gleixner 8783ac7fe5aSThomas Gleixner db = get_bucket((unsigned long) addr); 8793ac7fe5aSThomas Gleixner 880aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 8813ac7fe5aSThomas Gleixner 8823ac7fe5aSThomas Gleixner obj = lookup_object(addr, db); 8839bb63626SAndrzej Hajda if (!obj) { 8849bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 8859bb63626SAndrzej Hajda return; 8869bb63626SAndrzej Hajda } 8873ac7fe5aSThomas Gleixner 8883ac7fe5aSThomas Gleixner switch (obj->state) { 8899bb63626SAndrzej Hajda case ODEBUG_STATE_ACTIVE: 8909bb63626SAndrzej Hajda case ODEBUG_STATE_DESTROYED: 8919bb63626SAndrzej Hajda break; 8923ac7fe5aSThomas Gleixner case ODEBUG_STATE_NONE: 8933ac7fe5aSThomas Gleixner case ODEBUG_STATE_INIT: 8943ac7fe5aSThomas Gleixner case ODEBUG_STATE_INACTIVE: 8953ac7fe5aSThomas Gleixner obj->state = ODEBUG_STATE_DESTROYED; 8969bb63626SAndrzej Hajda fallthrough; 8973ac7fe5aSThomas Gleixner default: 898aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 8999bb63626SAndrzej Hajda return; 9009bb63626SAndrzej Hajda } 9019bb63626SAndrzej Hajda 9029bb63626SAndrzej Hajda o = *obj; 9039bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 9049bb63626SAndrzej Hajda debug_print_object(&o, "destroy"); 9059bb63626SAndrzej Hajda 9069bb63626SAndrzej Hajda if (o.state == ODEBUG_STATE_ACTIVE) 9079bb63626SAndrzej Hajda debug_object_fixup(descr->fixup_destroy, addr, o.state); 9083ac7fe5aSThomas Gleixner } 909f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_destroy); 9103ac7fe5aSThomas Gleixner 9113ac7fe5aSThomas Gleixner /** 9123ac7fe5aSThomas Gleixner * debug_object_free - debug checks when an object is freed 9133ac7fe5aSThomas Gleixner * @addr: address of the object 9143ac7fe5aSThomas Gleixner * @descr: pointer to an object specific debug description structure 9153ac7fe5aSThomas Gleixner */ 916aedcade6SStephen Boyd void debug_object_free(void *addr, const struct debug_obj_descr *descr) 9173ac7fe5aSThomas Gleixner { 9189bb63626SAndrzej Hajda struct debug_obj *obj, o; 9193ac7fe5aSThomas Gleixner struct debug_bucket *db; 9203ac7fe5aSThomas Gleixner unsigned long flags; 9213ac7fe5aSThomas Gleixner 9223ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 9233ac7fe5aSThomas Gleixner return; 9243ac7fe5aSThomas Gleixner 9253ac7fe5aSThomas Gleixner db = get_bucket((unsigned long) addr); 9263ac7fe5aSThomas Gleixner 927aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 9283ac7fe5aSThomas Gleixner 9293ac7fe5aSThomas Gleixner obj = lookup_object(addr, db); 9309bb63626SAndrzej Hajda if (!obj) { 9319bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 9329bb63626SAndrzej Hajda return; 9339bb63626SAndrzej Hajda } 9343ac7fe5aSThomas Gleixner 9353ac7fe5aSThomas Gleixner switch (obj->state) { 9363ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 9379bb63626SAndrzej Hajda break; 9383ac7fe5aSThomas Gleixner default: 9393ac7fe5aSThomas Gleixner hlist_del(&obj->node); 940aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 9413ac7fe5aSThomas Gleixner free_object(obj); 942673d62ccSVegard Nossum return; 9433ac7fe5aSThomas Gleixner } 9449bb63626SAndrzej Hajda 9459bb63626SAndrzej Hajda o = *obj; 946aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 9479bb63626SAndrzej Hajda debug_print_object(&o, "free"); 9489bb63626SAndrzej Hajda 9499bb63626SAndrzej Hajda debug_object_fixup(descr->fixup_free, addr, o.state); 9503ac7fe5aSThomas Gleixner } 951f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_free); 9523ac7fe5aSThomas Gleixner 953a5d8e467SMathieu Desnoyers /** 954b84d435cSChristine Chan * debug_object_assert_init - debug checks when object should be init-ed 955b84d435cSChristine Chan * @addr: address of the object 956b84d435cSChristine Chan * @descr: pointer to an object specific debug description structure 957b84d435cSChristine Chan */ 958aedcade6SStephen Boyd void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr) 959b84d435cSChristine Chan { 96063a75969SThomas Gleixner struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr }; 961b84d435cSChristine Chan struct debug_bucket *db; 962b84d435cSChristine Chan struct debug_obj *obj; 963b84d435cSChristine Chan unsigned long flags; 964b84d435cSChristine Chan 965b84d435cSChristine Chan if (!debug_objects_enabled) 966b84d435cSChristine Chan return; 967b84d435cSChristine Chan 9680af462f1SThomas Gleixner debug_objects_fill_pool(); 9690af462f1SThomas Gleixner 970b84d435cSChristine Chan db = get_bucket((unsigned long) addr); 971b84d435cSChristine Chan 972b84d435cSChristine Chan raw_spin_lock_irqsave(&db->lock, flags); 97363a75969SThomas Gleixner obj = lookup_object_or_alloc(addr, db, descr, false, true); 974b84d435cSChristine Chan raw_spin_unlock_irqrestore(&db->lock, flags); 97563a75969SThomas Gleixner if (likely(!IS_ERR_OR_NULL(obj))) 97663a75969SThomas Gleixner return; 97763a75969SThomas Gleixner 97863a75969SThomas Gleixner /* If NULL the allocation has hit OOM */ 97963a75969SThomas Gleixner if (!obj) { 98063a75969SThomas Gleixner debug_objects_oom(); 981b84d435cSChristine Chan return; 982b84d435cSChristine Chan } 983b84d435cSChristine Chan 98463a75969SThomas Gleixner /* Object is neither tracked nor static. It's not initialized. */ 98563a75969SThomas Gleixner debug_print_object(&o, "assert_init"); 98663a75969SThomas Gleixner debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE); 987b84d435cSChristine Chan } 988f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_assert_init); 989b84d435cSChristine Chan 990b84d435cSChristine Chan /** 991a5d8e467SMathieu Desnoyers * debug_object_active_state - debug checks object usage state machine 992a5d8e467SMathieu Desnoyers * @addr: address of the object 993a5d8e467SMathieu Desnoyers * @descr: pointer to an object specific debug description structure 994a5d8e467SMathieu Desnoyers * @expect: expected state 995a5d8e467SMathieu Desnoyers * @next: state to move to if expected state is found 996a5d8e467SMathieu Desnoyers */ 997a5d8e467SMathieu Desnoyers void 998aedcade6SStephen Boyd debug_object_active_state(void *addr, const struct debug_obj_descr *descr, 999a5d8e467SMathieu Desnoyers unsigned int expect, unsigned int next) 1000a5d8e467SMathieu Desnoyers { 10019bb63626SAndrzej Hajda struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr }; 1002a5d8e467SMathieu Desnoyers struct debug_bucket *db; 1003a5d8e467SMathieu Desnoyers struct debug_obj *obj; 1004a5d8e467SMathieu Desnoyers unsigned long flags; 1005a5d8e467SMathieu Desnoyers 1006a5d8e467SMathieu Desnoyers if (!debug_objects_enabled) 1007a5d8e467SMathieu Desnoyers return; 1008a5d8e467SMathieu Desnoyers 1009a5d8e467SMathieu Desnoyers db = get_bucket((unsigned long) addr); 1010a5d8e467SMathieu Desnoyers 1011a5d8e467SMathieu Desnoyers raw_spin_lock_irqsave(&db->lock, flags); 1012a5d8e467SMathieu Desnoyers 1013a5d8e467SMathieu Desnoyers obj = lookup_object(addr, db); 1014a5d8e467SMathieu Desnoyers if (obj) { 1015a5d8e467SMathieu Desnoyers switch (obj->state) { 1016a5d8e467SMathieu Desnoyers case ODEBUG_STATE_ACTIVE: 10179bb63626SAndrzej Hajda if (obj->astate != expect) 1018a5d8e467SMathieu Desnoyers break; 10199bb63626SAndrzej Hajda obj->astate = next; 10209bb63626SAndrzej Hajda raw_spin_unlock_irqrestore(&db->lock, flags); 10219bb63626SAndrzej Hajda return; 1022a5d8e467SMathieu Desnoyers default: 1023a5d8e467SMathieu Desnoyers break; 1024a5d8e467SMathieu Desnoyers } 10259bb63626SAndrzej Hajda o = *obj; 1026d5f34153SWaiman Long } 1027d5f34153SWaiman Long 1028d5f34153SWaiman Long raw_spin_unlock_irqrestore(&db->lock, flags); 1029a5d8e467SMathieu Desnoyers debug_print_object(&o, "active_state"); 1030a5d8e467SMathieu Desnoyers } 1031f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_active_state); 1032a5d8e467SMathieu Desnoyers 10333ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE 10343ac7fe5aSThomas Gleixner static void __debug_check_no_obj_freed(const void *address, unsigned long size) 10353ac7fe5aSThomas Gleixner { 10363ac7fe5aSThomas Gleixner unsigned long flags, oaddr, saddr, eaddr, paddr, chunks; 10379bb63626SAndrzej Hajda int cnt, objs_checked = 0; 10389bb63626SAndrzej Hajda struct debug_obj *obj, o; 10393ac7fe5aSThomas Gleixner struct debug_bucket *db; 10401ea9b98bSYang Shi struct hlist_node *tmp; 10413ac7fe5aSThomas Gleixner 10423ac7fe5aSThomas Gleixner saddr = (unsigned long) address; 10433ac7fe5aSThomas Gleixner eaddr = saddr + size; 10443ac7fe5aSThomas Gleixner paddr = saddr & ODEBUG_CHUNK_MASK; 10453ac7fe5aSThomas Gleixner chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1)); 10463ac7fe5aSThomas Gleixner chunks >>= ODEBUG_CHUNK_SHIFT; 10473ac7fe5aSThomas Gleixner 10483ac7fe5aSThomas Gleixner for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) { 10493ac7fe5aSThomas Gleixner db = get_bucket(paddr); 10503ac7fe5aSThomas Gleixner 10513ac7fe5aSThomas Gleixner repeat: 10523ac7fe5aSThomas Gleixner cnt = 0; 1053aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 1054b67bfe0dSSasha Levin hlist_for_each_entry_safe(obj, tmp, &db->list, node) { 10553ac7fe5aSThomas Gleixner cnt++; 10563ac7fe5aSThomas Gleixner oaddr = (unsigned long) obj->object; 10573ac7fe5aSThomas Gleixner if (oaddr < saddr || oaddr >= eaddr) 10583ac7fe5aSThomas Gleixner continue; 10593ac7fe5aSThomas Gleixner 10603ac7fe5aSThomas Gleixner switch (obj->state) { 10613ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 10629bb63626SAndrzej Hajda o = *obj; 1063aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 10649bb63626SAndrzej Hajda debug_print_object(&o, "free"); 10659bb63626SAndrzej Hajda debug_object_fixup(o.descr->fixup_free, (void *)oaddr, o.state); 10663ac7fe5aSThomas Gleixner goto repeat; 10673ac7fe5aSThomas Gleixner default: 10683ac7fe5aSThomas Gleixner hlist_del(&obj->node); 1069a7344a68SWaiman Long __free_object(obj); 10703ac7fe5aSThomas Gleixner break; 10713ac7fe5aSThomas Gleixner } 10723ac7fe5aSThomas Gleixner } 1073aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 1074673d62ccSVegard Nossum 10753ac7fe5aSThomas Gleixner if (cnt > debug_objects_maxchain) 10763ac7fe5aSThomas Gleixner debug_objects_maxchain = cnt; 1077bd9dcd04SYang Shi 1078bd9dcd04SYang Shi objs_checked += cnt; 10793ac7fe5aSThomas Gleixner } 1080bd9dcd04SYang Shi 1081bd9dcd04SYang Shi if (objs_checked > debug_objects_maxchecked) 1082bd9dcd04SYang Shi debug_objects_maxchecked = objs_checked; 10831ea9b98bSYang Shi 10841ea9b98bSYang Shi /* Schedule work to actually kmem_cache_free() objects */ 1085e18328ffSThomas Gleixner if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) { 1086a7344a68SWaiman Long WRITE_ONCE(obj_freeing, true); 1087a7344a68SWaiman Long schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY); 1088a7344a68SWaiman Long } 10893ac7fe5aSThomas Gleixner } 10903ac7fe5aSThomas Gleixner 10913ac7fe5aSThomas Gleixner void debug_check_no_obj_freed(const void *address, unsigned long size) 10923ac7fe5aSThomas Gleixner { 10933ac7fe5aSThomas Gleixner if (debug_objects_enabled) 10943ac7fe5aSThomas Gleixner __debug_check_no_obj_freed(address, size); 10953ac7fe5aSThomas Gleixner } 10963ac7fe5aSThomas Gleixner #endif 10973ac7fe5aSThomas Gleixner 10983ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_FS 10993ac7fe5aSThomas Gleixner 11003ac7fe5aSThomas Gleixner static int debug_stats_show(struct seq_file *m, void *v) 11013ac7fe5aSThomas Gleixner { 11022638345dSThomas Gleixner unsigned int cpu, pool_used, pcp_free = 0; 1103d86998b1SWaiman Long 11042638345dSThomas Gleixner /* 11052638345dSThomas Gleixner * pool_global.stats.cur_used is the number of batches currently 11062638345dSThomas Gleixner * handed out to per CPU pools. Convert it to number of objects 11072638345dSThomas Gleixner * and subtract the number of free objects in the per CPU pools. 11082638345dSThomas Gleixner * As this is lockless the number is an estimate. 11092638345dSThomas Gleixner */ 1110d86998b1SWaiman Long for_each_possible_cpu(cpu) 11112638345dSThomas Gleixner pcp_free += per_cpu(pool_pcpu.cnt, cpu); 11122638345dSThomas Gleixner 11132638345dSThomas Gleixner pool_used = data_race(pool_global.stats.cur_used); 11142638345dSThomas Gleixner pcp_free = min(pool_used, pcp_free); 11152638345dSThomas Gleixner pool_used -= pcp_free; 1116d86998b1SWaiman Long 11173ac7fe5aSThomas Gleixner seq_printf(m, "max_chain : %d\n", debug_objects_maxchain); 1118bd9dcd04SYang Shi seq_printf(m, "max_checked : %d\n", debug_objects_maxchecked); 11193ac7fe5aSThomas Gleixner seq_printf(m, "warnings : %d\n", debug_objects_warnings); 11203ac7fe5aSThomas Gleixner seq_printf(m, "fixups : %d\n", debug_objects_fixups); 11212638345dSThomas Gleixner seq_printf(m, "pool_free : %u\n", pool_count(&pool_global) + pcp_free); 11222638345dSThomas Gleixner seq_printf(m, "pool_pcp_free : %u\n", pcp_free); 11232638345dSThomas Gleixner seq_printf(m, "pool_min_free : %u\n", data_race(pool_global.stats.min_fill)); 11242638345dSThomas Gleixner seq_printf(m, "pool_used : %u\n", pool_used); 11252638345dSThomas Gleixner seq_printf(m, "pool_max_used : %u\n", data_race(pool_global.stats.max_used)); 11262638345dSThomas Gleixner seq_printf(m, "on_free_list : %u\n", pool_count(&pool_to_free)); 11270cad93c3SWaiman Long seq_printf(m, "objs_allocated: %d\n", debug_objects_allocated); 11280cad93c3SWaiman Long seq_printf(m, "objs_freed : %d\n", debug_objects_freed); 11293ac7fe5aSThomas Gleixner return 0; 11303ac7fe5aSThomas Gleixner } 11310f85c480SQinglang Miao DEFINE_SHOW_ATTRIBUTE(debug_stats); 11323ac7fe5aSThomas Gleixner 11333ac7fe5aSThomas Gleixner static int __init debug_objects_init_debugfs(void) 11343ac7fe5aSThomas Gleixner { 1135fecb0d95SGreg Kroah-Hartman struct dentry *dbgdir; 11363ac7fe5aSThomas Gleixner 11373ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 11383ac7fe5aSThomas Gleixner return 0; 11393ac7fe5aSThomas Gleixner 11403ac7fe5aSThomas Gleixner dbgdir = debugfs_create_dir("debug_objects", NULL); 11413ac7fe5aSThomas Gleixner 1142fecb0d95SGreg Kroah-Hartman debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops); 11433ac7fe5aSThomas Gleixner 11443ac7fe5aSThomas Gleixner return 0; 11453ac7fe5aSThomas Gleixner } 11463ac7fe5aSThomas Gleixner __initcall(debug_objects_init_debugfs); 11473ac7fe5aSThomas Gleixner 11483ac7fe5aSThomas Gleixner #else 11493ac7fe5aSThomas Gleixner static inline void debug_objects_init_debugfs(void) { } 11503ac7fe5aSThomas Gleixner #endif 11513ac7fe5aSThomas Gleixner 11523ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST 11533ac7fe5aSThomas Gleixner 11543ac7fe5aSThomas Gleixner /* Random data structure for the self test */ 11553ac7fe5aSThomas Gleixner struct self_test { 11563ac7fe5aSThomas Gleixner unsigned long dummy1[6]; 11573ac7fe5aSThomas Gleixner int static_init; 11583ac7fe5aSThomas Gleixner unsigned long dummy2[3]; 11593ac7fe5aSThomas Gleixner }; 11603ac7fe5aSThomas Gleixner 1161aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test; 11623ac7fe5aSThomas Gleixner 1163b9fdac7fSDu, Changbin static bool __init is_static_object(void *addr) 1164b9fdac7fSDu, Changbin { 1165b9fdac7fSDu, Changbin struct self_test *obj = addr; 1166b9fdac7fSDu, Changbin 1167b9fdac7fSDu, Changbin return obj->static_init; 1168b9fdac7fSDu, Changbin } 1169b9fdac7fSDu, Changbin 11703ac7fe5aSThomas Gleixner /* 11713ac7fe5aSThomas Gleixner * fixup_init is called when: 11723ac7fe5aSThomas Gleixner * - an active object is initialized 11733ac7fe5aSThomas Gleixner */ 1174b1e4d9d8SDu, Changbin static bool __init fixup_init(void *addr, enum debug_obj_state state) 11753ac7fe5aSThomas Gleixner { 11763ac7fe5aSThomas Gleixner struct self_test *obj = addr; 11773ac7fe5aSThomas Gleixner 11783ac7fe5aSThomas Gleixner switch (state) { 11793ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 11803ac7fe5aSThomas Gleixner debug_object_deactivate(obj, &descr_type_test); 11813ac7fe5aSThomas Gleixner debug_object_init(obj, &descr_type_test); 1182b1e4d9d8SDu, Changbin return true; 11833ac7fe5aSThomas Gleixner default: 1184b1e4d9d8SDu, Changbin return false; 11853ac7fe5aSThomas Gleixner } 11863ac7fe5aSThomas Gleixner } 11873ac7fe5aSThomas Gleixner 11883ac7fe5aSThomas Gleixner /* 11893ac7fe5aSThomas Gleixner * fixup_activate is called when: 11903ac7fe5aSThomas Gleixner * - an active object is activated 1191b9fdac7fSDu, Changbin * - an unknown non-static object is activated 11923ac7fe5aSThomas Gleixner */ 1193b1e4d9d8SDu, Changbin static bool __init fixup_activate(void *addr, enum debug_obj_state state) 11943ac7fe5aSThomas Gleixner { 11953ac7fe5aSThomas Gleixner struct self_test *obj = addr; 11963ac7fe5aSThomas Gleixner 11973ac7fe5aSThomas Gleixner switch (state) { 11983ac7fe5aSThomas Gleixner case ODEBUG_STATE_NOTAVAILABLE: 1199b1e4d9d8SDu, Changbin return true; 12003ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 12013ac7fe5aSThomas Gleixner debug_object_deactivate(obj, &descr_type_test); 12023ac7fe5aSThomas Gleixner debug_object_activate(obj, &descr_type_test); 1203b1e4d9d8SDu, Changbin return true; 12043ac7fe5aSThomas Gleixner 12053ac7fe5aSThomas Gleixner default: 1206b1e4d9d8SDu, Changbin return false; 12073ac7fe5aSThomas Gleixner } 12083ac7fe5aSThomas Gleixner } 12093ac7fe5aSThomas Gleixner 12103ac7fe5aSThomas Gleixner /* 12113ac7fe5aSThomas Gleixner * fixup_destroy is called when: 12123ac7fe5aSThomas Gleixner * - an active object is destroyed 12133ac7fe5aSThomas Gleixner */ 1214b1e4d9d8SDu, Changbin static bool __init fixup_destroy(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_destroy(obj, &descr_type_test); 1222b1e4d9d8SDu, Changbin return true; 12233ac7fe5aSThomas Gleixner default: 1224b1e4d9d8SDu, Changbin return false; 12253ac7fe5aSThomas Gleixner } 12263ac7fe5aSThomas Gleixner } 12273ac7fe5aSThomas Gleixner 12283ac7fe5aSThomas Gleixner /* 12293ac7fe5aSThomas Gleixner * fixup_free is called when: 12303ac7fe5aSThomas Gleixner * - an active object is freed 12313ac7fe5aSThomas Gleixner */ 1232b1e4d9d8SDu, Changbin static bool __init fixup_free(void *addr, enum debug_obj_state state) 12333ac7fe5aSThomas Gleixner { 12343ac7fe5aSThomas Gleixner struct self_test *obj = addr; 12353ac7fe5aSThomas Gleixner 12363ac7fe5aSThomas Gleixner switch (state) { 12373ac7fe5aSThomas Gleixner case ODEBUG_STATE_ACTIVE: 12383ac7fe5aSThomas Gleixner debug_object_deactivate(obj, &descr_type_test); 12393ac7fe5aSThomas Gleixner debug_object_free(obj, &descr_type_test); 1240b1e4d9d8SDu, Changbin return true; 12413ac7fe5aSThomas Gleixner default: 1242b1e4d9d8SDu, Changbin return false; 12433ac7fe5aSThomas Gleixner } 12443ac7fe5aSThomas Gleixner } 12453ac7fe5aSThomas Gleixner 12461fb2f77cSHenrik Kretzschmar static int __init 12473ac7fe5aSThomas Gleixner check_results(void *addr, enum debug_obj_state state, int fixups, int warnings) 12483ac7fe5aSThomas Gleixner { 12493ac7fe5aSThomas Gleixner struct debug_bucket *db; 12503ac7fe5aSThomas Gleixner struct debug_obj *obj; 12513ac7fe5aSThomas Gleixner unsigned long flags; 12523ac7fe5aSThomas Gleixner int res = -EINVAL; 12533ac7fe5aSThomas Gleixner 12543ac7fe5aSThomas Gleixner db = get_bucket((unsigned long) addr); 12553ac7fe5aSThomas Gleixner 1256aef9cb05SThomas Gleixner raw_spin_lock_irqsave(&db->lock, flags); 12573ac7fe5aSThomas Gleixner 12583ac7fe5aSThomas Gleixner obj = lookup_object(addr, db); 12593ac7fe5aSThomas Gleixner if (!obj && state != ODEBUG_STATE_NONE) { 12605cd2b459SArjan van de Ven WARN(1, KERN_ERR "ODEBUG: selftest object not found\n"); 12613ac7fe5aSThomas Gleixner goto out; 12623ac7fe5aSThomas Gleixner } 12633ac7fe5aSThomas Gleixner if (obj && obj->state != state) { 12645cd2b459SArjan van de Ven WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n", 12653ac7fe5aSThomas Gleixner obj->state, state); 12663ac7fe5aSThomas Gleixner goto out; 12673ac7fe5aSThomas Gleixner } 12683ac7fe5aSThomas Gleixner if (fixups != debug_objects_fixups) { 12695cd2b459SArjan van de Ven WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n", 12703ac7fe5aSThomas Gleixner fixups, debug_objects_fixups); 12713ac7fe5aSThomas Gleixner goto out; 12723ac7fe5aSThomas Gleixner } 12733ac7fe5aSThomas Gleixner if (warnings != debug_objects_warnings) { 12745cd2b459SArjan van de Ven WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n", 12753ac7fe5aSThomas Gleixner warnings, debug_objects_warnings); 12763ac7fe5aSThomas Gleixner goto out; 12773ac7fe5aSThomas Gleixner } 12783ac7fe5aSThomas Gleixner res = 0; 12793ac7fe5aSThomas Gleixner out: 1280aef9cb05SThomas Gleixner raw_spin_unlock_irqrestore(&db->lock, flags); 12813ac7fe5aSThomas Gleixner if (res) 1282661cc28bSThomas Gleixner debug_objects_enabled = false; 12833ac7fe5aSThomas Gleixner return res; 12843ac7fe5aSThomas Gleixner } 12853ac7fe5aSThomas Gleixner 1286aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test = { 12873ac7fe5aSThomas Gleixner .name = "selftest", 1288b9fdac7fSDu, Changbin .is_static_object = is_static_object, 12893ac7fe5aSThomas Gleixner .fixup_init = fixup_init, 12903ac7fe5aSThomas Gleixner .fixup_activate = fixup_activate, 12913ac7fe5aSThomas Gleixner .fixup_destroy = fixup_destroy, 12923ac7fe5aSThomas Gleixner .fixup_free = fixup_free, 12933ac7fe5aSThomas Gleixner }; 12943ac7fe5aSThomas Gleixner 12953ac7fe5aSThomas Gleixner static __initdata struct self_test obj = { .static_init = 0 }; 12963ac7fe5aSThomas Gleixner 129755fb412eSThomas Gleixner static bool __init debug_objects_selftest(void) 12983ac7fe5aSThomas Gleixner { 12993ac7fe5aSThomas Gleixner int fixups, oldfixups, warnings, oldwarnings; 13003ac7fe5aSThomas Gleixner unsigned long flags; 13013ac7fe5aSThomas Gleixner 13023ac7fe5aSThomas Gleixner local_irq_save(flags); 13033ac7fe5aSThomas Gleixner 13043ac7fe5aSThomas Gleixner fixups = oldfixups = debug_objects_fixups; 13053ac7fe5aSThomas Gleixner warnings = oldwarnings = debug_objects_warnings; 13063ac7fe5aSThomas Gleixner descr_test = &descr_type_test; 13073ac7fe5aSThomas Gleixner 13083ac7fe5aSThomas Gleixner debug_object_init(&obj, &descr_type_test); 13093ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings)) 13103ac7fe5aSThomas Gleixner goto out; 13113ac7fe5aSThomas Gleixner debug_object_activate(&obj, &descr_type_test); 13123ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) 13133ac7fe5aSThomas Gleixner goto out; 13143ac7fe5aSThomas Gleixner debug_object_activate(&obj, &descr_type_test); 13153ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings)) 13163ac7fe5aSThomas Gleixner goto out; 13173ac7fe5aSThomas Gleixner debug_object_deactivate(&obj, &descr_type_test); 13183ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings)) 13193ac7fe5aSThomas Gleixner goto out; 13203ac7fe5aSThomas Gleixner debug_object_destroy(&obj, &descr_type_test); 13213ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings)) 13223ac7fe5aSThomas Gleixner goto out; 13233ac7fe5aSThomas Gleixner debug_object_init(&obj, &descr_type_test); 13243ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) 13253ac7fe5aSThomas Gleixner goto out; 13263ac7fe5aSThomas Gleixner debug_object_activate(&obj, &descr_type_test); 13273ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) 13283ac7fe5aSThomas Gleixner goto out; 13293ac7fe5aSThomas Gleixner debug_object_deactivate(&obj, &descr_type_test); 13303ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) 13313ac7fe5aSThomas Gleixner goto out; 13323ac7fe5aSThomas Gleixner debug_object_free(&obj, &descr_type_test); 13333ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings)) 13343ac7fe5aSThomas Gleixner goto out; 13353ac7fe5aSThomas Gleixner 13363ac7fe5aSThomas Gleixner obj.static_init = 1; 13373ac7fe5aSThomas Gleixner debug_object_activate(&obj, &descr_type_test); 13389f78ff00SStephen Boyd if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) 13393ac7fe5aSThomas Gleixner goto out; 13403ac7fe5aSThomas Gleixner debug_object_init(&obj, &descr_type_test); 13413ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings)) 13423ac7fe5aSThomas Gleixner goto out; 13433ac7fe5aSThomas Gleixner debug_object_free(&obj, &descr_type_test); 13443ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings)) 13453ac7fe5aSThomas Gleixner goto out; 13463ac7fe5aSThomas Gleixner 13473ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE 13483ac7fe5aSThomas Gleixner debug_object_init(&obj, &descr_type_test); 13493ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings)) 13503ac7fe5aSThomas Gleixner goto out; 13513ac7fe5aSThomas Gleixner debug_object_activate(&obj, &descr_type_test); 13523ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) 13533ac7fe5aSThomas Gleixner goto out; 13543ac7fe5aSThomas Gleixner __debug_check_no_obj_freed(&obj, sizeof(obj)); 13553ac7fe5aSThomas Gleixner if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings)) 13563ac7fe5aSThomas Gleixner goto out; 13573ac7fe5aSThomas Gleixner #endif 1358719e4843SFabian Frederick pr_info("selftest passed\n"); 13593ac7fe5aSThomas Gleixner 13603ac7fe5aSThomas Gleixner out: 13613ac7fe5aSThomas Gleixner debug_objects_fixups = oldfixups; 13623ac7fe5aSThomas Gleixner debug_objects_warnings = oldwarnings; 13633ac7fe5aSThomas Gleixner descr_test = NULL; 13643ac7fe5aSThomas Gleixner 13653ac7fe5aSThomas Gleixner local_irq_restore(flags); 1366661cc28bSThomas Gleixner return debug_objects_enabled; 13673ac7fe5aSThomas Gleixner } 13683ac7fe5aSThomas Gleixner #else 136955fb412eSThomas Gleixner static inline bool debug_objects_selftest(void) { return true; } 13703ac7fe5aSThomas Gleixner #endif 13713ac7fe5aSThomas Gleixner 13723ac7fe5aSThomas Gleixner /* 13733ac7fe5aSThomas Gleixner * Called during early boot to initialize the hash buckets and link 13743ac7fe5aSThomas Gleixner * the static object pool objects into the poll list. After this call 13753ac7fe5aSThomas Gleixner * the object tracker is fully operational. 13763ac7fe5aSThomas Gleixner */ 13773ac7fe5aSThomas Gleixner void __init debug_objects_early_init(void) 13783ac7fe5aSThomas Gleixner { 13793ac7fe5aSThomas Gleixner int i; 13803ac7fe5aSThomas Gleixner 13813ac7fe5aSThomas Gleixner for (i = 0; i < ODEBUG_HASH_SIZE; i++) 1382aef9cb05SThomas Gleixner raw_spin_lock_init(&obj_hash[i].lock); 13833ac7fe5aSThomas Gleixner 1384cb58d190SThomas Gleixner /* Keep early boot simple and add everything to the boot list */ 13853ac7fe5aSThomas Gleixner for (i = 0; i < ODEBUG_POOL_SIZE; i++) 1386cb58d190SThomas Gleixner hlist_add_head(&obj_static_pool[i].node, &pool_boot); 13873ac7fe5aSThomas Gleixner } 13883ac7fe5aSThomas Gleixner 13893ac7fe5aSThomas Gleixner /* 139055fb412eSThomas Gleixner * Convert the statically allocated objects to dynamic ones. 139155fb412eSThomas Gleixner * debug_objects_mem_init() is called early so only one CPU is up and 139255fb412eSThomas Gleixner * interrupts are disabled, which means it is safe to replace the active 139355fb412eSThomas Gleixner * object references. 13941be1cb7bSThomas Gleixner */ 139555fb412eSThomas Gleixner static bool __init debug_objects_replace_static_objects(struct kmem_cache *cache) 13961be1cb7bSThomas Gleixner { 13971be1cb7bSThomas Gleixner struct debug_bucket *db = obj_hash; 139855fb412eSThomas Gleixner struct hlist_node *tmp; 1399aebbfe07SThomas Gleixner struct debug_obj *obj; 14001be1cb7bSThomas Gleixner HLIST_HEAD(objects); 1401241463f4SThomas Gleixner int i; 14021be1cb7bSThomas Gleixner 1403aebbfe07SThomas Gleixner for (i = 0; i < ODEBUG_POOL_SIZE; i += ODEBUG_BATCH_SIZE) { 1404aebbfe07SThomas Gleixner if (!kmem_alloc_batch(&objects, cache, GFP_KERNEL)) 14051be1cb7bSThomas Gleixner goto free; 1406aebbfe07SThomas Gleixner pool_push_batch(&pool_global, &objects); 14071be1cb7bSThomas Gleixner } 14081be1cb7bSThomas Gleixner 1409aebbfe07SThomas Gleixner /* Disconnect the boot pool. */ 1410cb58d190SThomas Gleixner pool_boot.first = NULL; 14111be1cb7bSThomas Gleixner 14121be1cb7bSThomas Gleixner /* Replace the active object references */ 14131be1cb7bSThomas Gleixner for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) { 14141be1cb7bSThomas Gleixner hlist_move_list(&db->list, &objects); 14151be1cb7bSThomas Gleixner 1416b67bfe0dSSasha Levin hlist_for_each_entry(obj, &objects, node) { 1417aebbfe07SThomas Gleixner struct debug_obj *new = pcpu_alloc(); 1418aebbfe07SThomas Gleixner 14191be1cb7bSThomas Gleixner /* copy object data */ 14201be1cb7bSThomas Gleixner *new = *obj; 14211be1cb7bSThomas Gleixner hlist_add_head(&new->node, &db->list); 14221be1cb7bSThomas Gleixner } 14231be1cb7bSThomas Gleixner } 142455fb412eSThomas Gleixner return true; 14251be1cb7bSThomas Gleixner free: 142649a5cb82SThomas Gleixner /* Can't use free_object_list() as the cache is not populated yet */ 1427aebbfe07SThomas Gleixner hlist_for_each_entry_safe(obj, tmp, &pool_global.objects, node) { 14281be1cb7bSThomas Gleixner hlist_del(&obj->node); 142955fb412eSThomas Gleixner kmem_cache_free(cache, obj); 14301be1cb7bSThomas Gleixner } 143155fb412eSThomas Gleixner return false; 14321be1cb7bSThomas Gleixner } 14331be1cb7bSThomas Gleixner 14341be1cb7bSThomas Gleixner /* 14353ac7fe5aSThomas Gleixner * Called after the kmem_caches are functional to setup a dedicated 14363ac7fe5aSThomas Gleixner * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag 14373ac7fe5aSThomas Gleixner * prevents that the debug code is called on kmem_cache_free() for the 14383ac7fe5aSThomas Gleixner * debug tracker objects to avoid recursive calls. 14393ac7fe5aSThomas Gleixner */ 14403ac7fe5aSThomas Gleixner void __init debug_objects_mem_init(void) 14413ac7fe5aSThomas Gleixner { 144255fb412eSThomas Gleixner struct kmem_cache *cache; 14433f397bf9SThomas Gleixner int extras; 1444d86998b1SWaiman Long 14453ac7fe5aSThomas Gleixner if (!debug_objects_enabled) 14463ac7fe5aSThomas Gleixner return; 14473ac7fe5aSThomas Gleixner 144855fb412eSThomas Gleixner if (!debug_objects_selftest()) 1449eabb7f1aSwuchi return; 145055fb412eSThomas Gleixner 145155fb412eSThomas Gleixner cache = kmem_cache_create("debug_objects_cache", sizeof (struct debug_obj), 0, 145255fb412eSThomas Gleixner SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE, NULL); 145355fb412eSThomas Gleixner 145455fb412eSThomas Gleixner if (!cache || !debug_objects_replace_static_objects(cache)) { 1455661cc28bSThomas Gleixner debug_objects_enabled = false; 145655fb412eSThomas Gleixner pr_warn("Out of memory.\n"); 145755fb412eSThomas Gleixner return; 145855fb412eSThomas Gleixner } 145955fb412eSThomas Gleixner 146055fb412eSThomas Gleixner /* 146155fb412eSThomas Gleixner * Adjust the thresholds for allocating and freeing objects 146255fb412eSThomas Gleixner * according to the number of possible CPUs available in the 146355fb412eSThomas Gleixner * system. 146455fb412eSThomas Gleixner */ 146555fb412eSThomas Gleixner extras = num_possible_cpus() * ODEBUG_BATCH_SIZE; 146696a9a042SThomas Gleixner pool_global.max_cnt += extras; 146796a9a042SThomas Gleixner pool_global.min_cnt += extras; 146855fb412eSThomas Gleixner 146955fb412eSThomas Gleixner /* Everything worked. Expose the cache */ 147055fb412eSThomas Gleixner obj_cache = cache; 147114077b9eSThomas Gleixner static_branch_enable(&obj_cache_enabled); 1472634d61f4SWaiman Long 147388451f2cSZqiang #ifdef CONFIG_HOTPLUG_CPU 147488451f2cSZqiang cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL, 147588451f2cSZqiang object_cpu_offline); 147688451f2cSZqiang #endif 147755fb412eSThomas Gleixner return; 14783ac7fe5aSThomas Gleixner } 1479