Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic infrastructure for lifetime debugging of objects. |
| 3 | * |
| 4 | * Started by Thomas Gleixner |
| 5 | * |
| 6 | * Copyright (C) 2008, Thomas Gleixner <tglx@linutronix.de> |
| 7 | * |
| 8 | * For licencing details see kernel-base/COPYING |
| 9 | */ |
Fabian Frederick | 719e484 | 2014-06-04 16:06:04 -0700 | [diff] [blame] | 10 | |
| 11 | #define pr_fmt(fmt) "ODEBUG: " fmt |
| 12 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 13 | #include <linux/debugobjects.h> |
| 14 | #include <linux/interrupt.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 15 | #include <linux/sched.h> |
Ingo Molnar | 68db0cf | 2017-02-08 18:51:37 +0100 | [diff] [blame] | 16 | #include <linux/sched/task_stack.h> |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 17 | #include <linux/seq_file.h> |
| 18 | #include <linux/debugfs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 20 | #include <linux/hash.h> |
Waiman Long | caba4cb | 2017-08-14 09:52:13 -0400 | [diff] [blame] | 21 | #include <linux/kmemleak.h> |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 22 | |
| 23 | #define ODEBUG_HASH_BITS 14 |
| 24 | #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS) |
| 25 | |
Christian Borntraeger | 0b6ec8c | 2016-01-27 15:37:58 +0100 | [diff] [blame] | 26 | #define ODEBUG_POOL_SIZE 1024 |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 27 | #define ODEBUG_POOL_MIN_LEVEL 256 |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 28 | #define ODEBUG_POOL_PERCPU_SIZE 64 |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 29 | #define ODEBUG_BATCH_SIZE 16 |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 30 | |
| 31 | #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT |
| 32 | #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT) |
| 33 | #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1)) |
| 34 | |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 35 | /* |
| 36 | * We limit the freeing of debug objects via workqueue at a maximum |
| 37 | * frequency of 10Hz and about 1024 objects for each freeing operation. |
| 38 | * So it is freeing at most 10k debug objects per second. |
| 39 | */ |
| 40 | #define ODEBUG_FREE_WORK_MAX 1024 |
| 41 | #define ODEBUG_FREE_WORK_DELAY DIV_ROUND_UP(HZ, 10) |
| 42 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 43 | struct debug_bucket { |
| 44 | struct hlist_head list; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 45 | raw_spinlock_t lock; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 46 | }; |
| 47 | |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 48 | /* |
| 49 | * Debug object percpu free list |
| 50 | * Access is protected by disabling irq |
| 51 | */ |
| 52 | struct debug_percpu_free { |
| 53 | struct hlist_head free_objs; |
| 54 | int obj_free; |
| 55 | }; |
| 56 | |
| 57 | static DEFINE_PER_CPU(struct debug_percpu_free, percpu_obj_pool); |
| 58 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 59 | static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE]; |
| 60 | |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 61 | static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 62 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 63 | static DEFINE_RAW_SPINLOCK(pool_lock); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 64 | |
| 65 | static HLIST_HEAD(obj_pool); |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 66 | static HLIST_HEAD(obj_to_free); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 67 | |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 68 | /* |
| 69 | * Because of the presence of percpu free pools, obj_pool_free will |
| 70 | * under-count those in the percpu free pools. Similarly, obj_pool_used |
| 71 | * will over-count those in the percpu free pools. Adjustments will be |
| 72 | * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used |
| 73 | * can be off. |
| 74 | */ |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 75 | static int obj_pool_min_free = ODEBUG_POOL_SIZE; |
| 76 | static int obj_pool_free = ODEBUG_POOL_SIZE; |
| 77 | static int obj_pool_used; |
| 78 | static int obj_pool_max_used; |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 79 | static bool obj_freeing; |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 80 | /* The number of objs on the global free list */ |
| 81 | static int obj_nr_tofree; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 82 | |
| 83 | static int debug_objects_maxchain __read_mostly; |
Arnd Bergmann | 163cf84 | 2018-03-13 14:18:46 +0100 | [diff] [blame] | 84 | static int __maybe_unused debug_objects_maxchecked __read_mostly; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 85 | static int debug_objects_fixups __read_mostly; |
| 86 | static int debug_objects_warnings __read_mostly; |
Ingo Molnar | 3ae7020 | 2008-11-26 10:02:00 +0100 | [diff] [blame] | 87 | static int debug_objects_enabled __read_mostly |
| 88 | = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT; |
Waiman Long | 97dd552 | 2017-01-05 15:17:04 -0500 | [diff] [blame] | 89 | static int debug_objects_pool_size __read_mostly |
| 90 | = ODEBUG_POOL_SIZE; |
| 91 | static int debug_objects_pool_min_level __read_mostly |
| 92 | = ODEBUG_POOL_MIN_LEVEL; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 93 | static struct debug_obj_descr *descr_test __read_mostly; |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 94 | static struct kmem_cache *obj_cache __read_mostly; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 95 | |
Waiman Long | c4b73aa | 2017-01-05 15:17:03 -0500 | [diff] [blame] | 96 | /* |
Waiman Long | 0cad93c | 2017-02-07 16:40:30 -0500 | [diff] [blame] | 97 | * Track numbers of kmem_cache_alloc()/free() calls done. |
Waiman Long | c4b73aa | 2017-01-05 15:17:03 -0500 | [diff] [blame] | 98 | */ |
Waiman Long | 0cad93c | 2017-02-07 16:40:30 -0500 | [diff] [blame] | 99 | static int debug_objects_allocated; |
Waiman Long | c4b73aa | 2017-01-05 15:17:03 -0500 | [diff] [blame] | 100 | static int debug_objects_freed; |
| 101 | |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 102 | static void free_obj_work(struct work_struct *work); |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 103 | static DECLARE_DELAYED_WORK(debug_obj_work, free_obj_work); |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 104 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 105 | static int __init enable_object_debug(char *str) |
| 106 | { |
| 107 | debug_objects_enabled = 1; |
| 108 | return 0; |
| 109 | } |
Kyle McMartin | 3e8ebb5 | 2009-03-01 20:41:41 -0500 | [diff] [blame] | 110 | |
| 111 | static int __init disable_object_debug(char *str) |
| 112 | { |
| 113 | debug_objects_enabled = 0; |
| 114 | return 0; |
| 115 | } |
| 116 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 117 | early_param("debug_objects", enable_object_debug); |
Kyle McMartin | 3e8ebb5 | 2009-03-01 20:41:41 -0500 | [diff] [blame] | 118 | early_param("no_debug_objects", disable_object_debug); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 119 | |
| 120 | static const char *obj_states[ODEBUG_STATE_MAX] = { |
| 121 | [ODEBUG_STATE_NONE] = "none", |
| 122 | [ODEBUG_STATE_INIT] = "initialized", |
| 123 | [ODEBUG_STATE_INACTIVE] = "inactive", |
| 124 | [ODEBUG_STATE_ACTIVE] = "active", |
| 125 | [ODEBUG_STATE_DESTROYED] = "destroyed", |
| 126 | [ODEBUG_STATE_NOTAVAILABLE] = "not available", |
| 127 | }; |
| 128 | |
Thomas Gleixner | 1fda107 | 2012-04-11 11:52:18 +0200 | [diff] [blame] | 129 | static void fill_pool(void) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 130 | { |
| 131 | gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN; |
Waiman Long | d26bf50 | 2019-05-20 10:14:48 -0400 | [diff] [blame] | 132 | struct debug_obj *obj; |
Vegard Nossum | 50db04dd | 2008-06-15 00:47:36 +0200 | [diff] [blame] | 133 | unsigned long flags; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 134 | |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 135 | if (likely(READ_ONCE(obj_pool_free) >= debug_objects_pool_min_level)) |
Thomas Gleixner | 1fda107 | 2012-04-11 11:52:18 +0200 | [diff] [blame] | 136 | return; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 137 | |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 138 | /* |
| 139 | * Reuse objs from the global free list; they will be reinitialized |
| 140 | * when allocating. |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 141 | * |
| 142 | * Both obj_nr_tofree and obj_pool_free are checked locklessly; the |
| 143 | * READ_ONCE()s pair with the WRITE_ONCE()s in pool_lock critical |
| 144 | * sections. |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 145 | */ |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 146 | while (READ_ONCE(obj_nr_tofree) && (READ_ONCE(obj_pool_free) < obj_pool_min_free)) { |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 147 | raw_spin_lock_irqsave(&pool_lock, flags); |
| 148 | /* |
| 149 | * Recheck with the lock held as the worker thread might have |
| 150 | * won the race and freed the global free list already. |
| 151 | */ |
Waiman Long | d26bf50 | 2019-05-20 10:14:48 -0400 | [diff] [blame] | 152 | while (obj_nr_tofree && (obj_pool_free < obj_pool_min_free)) { |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 153 | obj = hlist_entry(obj_to_free.first, typeof(*obj), node); |
| 154 | hlist_del(&obj->node); |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 155 | WRITE_ONCE(obj_nr_tofree, obj_nr_tofree - 1); |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 156 | hlist_add_head(&obj->node, &obj_pool); |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 157 | WRITE_ONCE(obj_pool_free, obj_pool_free + 1); |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 158 | } |
| 159 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
| 160 | } |
| 161 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 162 | if (unlikely(!obj_cache)) |
Thomas Gleixner | 1fda107 | 2012-04-11 11:52:18 +0200 | [diff] [blame] | 163 | return; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 164 | |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 165 | while (READ_ONCE(obj_pool_free) < debug_objects_pool_min_level) { |
Waiman Long | d26bf50 | 2019-05-20 10:14:48 -0400 | [diff] [blame] | 166 | struct debug_obj *new[ODEBUG_BATCH_SIZE]; |
| 167 | int cnt; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 168 | |
Waiman Long | d26bf50 | 2019-05-20 10:14:48 -0400 | [diff] [blame] | 169 | for (cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) { |
| 170 | new[cnt] = kmem_cache_zalloc(obj_cache, gfp); |
| 171 | if (!new[cnt]) |
| 172 | break; |
| 173 | } |
| 174 | if (!cnt) |
Dan Carpenter | 3340808 | 2012-04-18 14:28:10 +0300 | [diff] [blame] | 175 | return; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 176 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 177 | raw_spin_lock_irqsave(&pool_lock, flags); |
Waiman Long | d26bf50 | 2019-05-20 10:14:48 -0400 | [diff] [blame] | 178 | while (cnt) { |
| 179 | hlist_add_head(&new[--cnt]->node, &obj_pool); |
| 180 | debug_objects_allocated++; |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 181 | WRITE_ONCE(obj_pool_free, obj_pool_free + 1); |
Waiman Long | d26bf50 | 2019-05-20 10:14:48 -0400 | [diff] [blame] | 182 | } |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 183 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 184 | } |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Lookup an object in the hash bucket. |
| 189 | */ |
| 190 | static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b) |
| 191 | { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 192 | struct debug_obj *obj; |
| 193 | int cnt = 0; |
| 194 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 195 | hlist_for_each_entry(obj, &b->list, node) { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 196 | cnt++; |
| 197 | if (obj->object == addr) |
| 198 | return obj; |
| 199 | } |
| 200 | if (cnt > debug_objects_maxchain) |
| 201 | debug_objects_maxchain = cnt; |
| 202 | |
| 203 | return NULL; |
| 204 | } |
| 205 | |
| 206 | /* |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 207 | * Allocate a new object from the hlist |
| 208 | */ |
| 209 | static struct debug_obj *__alloc_object(struct hlist_head *list) |
| 210 | { |
| 211 | struct debug_obj *obj = NULL; |
| 212 | |
| 213 | if (list->first) { |
| 214 | obj = hlist_entry(list->first, typeof(*obj), node); |
| 215 | hlist_del(&obj->node); |
| 216 | } |
| 217 | |
| 218 | return obj; |
| 219 | } |
| 220 | |
| 221 | /* |
Vegard Nossum | 50db04dd | 2008-06-15 00:47:36 +0200 | [diff] [blame] | 222 | * Allocate a new object. If the pool is empty, switch off the debugger. |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 223 | * Must be called with interrupts disabled. |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 224 | */ |
| 225 | static struct debug_obj * |
| 226 | alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr) |
| 227 | { |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 228 | struct debug_percpu_free *percpu_pool = this_cpu_ptr(&percpu_obj_pool); |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 229 | struct debug_obj *obj; |
| 230 | |
| 231 | if (likely(obj_cache)) { |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 232 | obj = __alloc_object(&percpu_pool->free_objs); |
| 233 | if (obj) { |
| 234 | percpu_pool->obj_free--; |
| 235 | goto init_obj; |
| 236 | } |
| 237 | } |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 238 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 239 | raw_spin_lock(&pool_lock); |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 240 | obj = __alloc_object(&obj_pool); |
| 241 | if (obj) { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 242 | obj_pool_used++; |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 243 | WRITE_ONCE(obj_pool_free, obj_pool_free - 1); |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 244 | |
| 245 | /* |
| 246 | * Looking ahead, allocate one batch of debug objects and |
| 247 | * put them into the percpu free pool. |
| 248 | */ |
| 249 | if (likely(obj_cache)) { |
| 250 | int i; |
| 251 | |
| 252 | for (i = 0; i < ODEBUG_BATCH_SIZE; i++) { |
| 253 | struct debug_obj *obj2; |
| 254 | |
| 255 | obj2 = __alloc_object(&obj_pool); |
| 256 | if (!obj2) |
| 257 | break; |
| 258 | hlist_add_head(&obj2->node, |
| 259 | &percpu_pool->free_objs); |
| 260 | percpu_pool->obj_free++; |
| 261 | obj_pool_used++; |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 262 | WRITE_ONCE(obj_pool_free, obj_pool_free - 1); |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 263 | } |
| 264 | } |
| 265 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 266 | if (obj_pool_used > obj_pool_max_used) |
| 267 | obj_pool_max_used = obj_pool_used; |
| 268 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 269 | if (obj_pool_free < obj_pool_min_free) |
| 270 | obj_pool_min_free = obj_pool_free; |
| 271 | } |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 272 | raw_spin_unlock(&pool_lock); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 273 | |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 274 | init_obj: |
| 275 | if (obj) { |
| 276 | obj->object = addr; |
| 277 | obj->descr = descr; |
| 278 | obj->state = ODEBUG_STATE_NONE; |
| 279 | obj->astate = 0; |
| 280 | hlist_add_head(&obj->node, &b->list); |
| 281 | } |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 282 | return obj; |
| 283 | } |
| 284 | |
| 285 | /* |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 286 | * workqueue function to free objects. |
Waiman Long | 858274b | 2017-01-05 15:17:05 -0500 | [diff] [blame] | 287 | * |
| 288 | * To reduce contention on the global pool_lock, the actual freeing of |
Yang Shi | 636e197 | 2018-02-06 07:18:27 +0800 | [diff] [blame] | 289 | * debug objects will be delayed if the pool_lock is busy. |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 290 | */ |
| 291 | static void free_obj_work(struct work_struct *work) |
| 292 | { |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 293 | struct hlist_node *tmp; |
| 294 | struct debug_obj *obj; |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 295 | unsigned long flags; |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 296 | HLIST_HEAD(tofree); |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 297 | |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 298 | WRITE_ONCE(obj_freeing, false); |
Waiman Long | 858274b | 2017-01-05 15:17:05 -0500 | [diff] [blame] | 299 | if (!raw_spin_trylock_irqsave(&pool_lock, flags)) |
| 300 | return; |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 301 | |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 302 | if (obj_pool_free >= debug_objects_pool_size) |
| 303 | goto free_objs; |
| 304 | |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 305 | /* |
| 306 | * The objs on the pool list might be allocated before the work is |
| 307 | * run, so recheck if pool list it full or not, if not fill pool |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 308 | * list from the global free list. As it is likely that a workload |
| 309 | * may be gearing up to use more and more objects, don't free any |
| 310 | * of them until the next round. |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 311 | */ |
| 312 | while (obj_nr_tofree && obj_pool_free < debug_objects_pool_size) { |
| 313 | obj = hlist_entry(obj_to_free.first, typeof(*obj), node); |
| 314 | hlist_del(&obj->node); |
| 315 | hlist_add_head(&obj->node, &obj_pool); |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 316 | WRITE_ONCE(obj_pool_free, obj_pool_free + 1); |
| 317 | WRITE_ONCE(obj_nr_tofree, obj_nr_tofree - 1); |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 318 | } |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 319 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
| 320 | return; |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 321 | |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 322 | free_objs: |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 323 | /* |
| 324 | * Pool list is already full and there are still objs on the free |
| 325 | * list. Move remaining free objs to a temporary list to free the |
| 326 | * memory outside the pool_lock held region. |
| 327 | */ |
| 328 | if (obj_nr_tofree) { |
| 329 | hlist_move_list(&obj_to_free, &tofree); |
Arnd Bergmann | 0414818 | 2018-02-22 16:52:58 +0100 | [diff] [blame] | 330 | debug_objects_freed += obj_nr_tofree; |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 331 | WRITE_ONCE(obj_nr_tofree, 0); |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 332 | } |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 333 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
Yang Shi | 36c4ead | 2018-02-06 07:18:26 +0800 | [diff] [blame] | 334 | |
| 335 | hlist_for_each_entry_safe(obj, tmp, &tofree, node) { |
| 336 | hlist_del(&obj->node); |
| 337 | kmem_cache_free(obj_cache, obj); |
| 338 | } |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 339 | } |
| 340 | |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 341 | static void __free_object(struct debug_obj *obj) |
Yang Shi | 636e197 | 2018-02-06 07:18:27 +0800 | [diff] [blame] | 342 | { |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 343 | struct debug_obj *objs[ODEBUG_BATCH_SIZE]; |
| 344 | struct debug_percpu_free *percpu_pool; |
| 345 | int lookahead_count = 0; |
Yang Shi | 636e197 | 2018-02-06 07:18:27 +0800 | [diff] [blame] | 346 | unsigned long flags; |
| 347 | bool work; |
| 348 | |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 349 | local_irq_save(flags); |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 350 | if (!obj_cache) |
| 351 | goto free_to_obj_pool; |
| 352 | |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 353 | /* |
| 354 | * Try to free it into the percpu pool first. |
| 355 | */ |
| 356 | percpu_pool = this_cpu_ptr(&percpu_obj_pool); |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 357 | if (percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) { |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 358 | hlist_add_head(&obj->node, &percpu_pool->free_objs); |
| 359 | percpu_pool->obj_free++; |
| 360 | local_irq_restore(flags); |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 361 | return; |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 362 | } |
| 363 | |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 364 | /* |
| 365 | * As the percpu pool is full, look ahead and pull out a batch |
| 366 | * of objects from the percpu pool and free them as well. |
| 367 | */ |
| 368 | for (; lookahead_count < ODEBUG_BATCH_SIZE; lookahead_count++) { |
| 369 | objs[lookahead_count] = __alloc_object(&percpu_pool->free_objs); |
| 370 | if (!objs[lookahead_count]) |
| 371 | break; |
| 372 | percpu_pool->obj_free--; |
| 373 | } |
| 374 | |
| 375 | free_to_obj_pool: |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 376 | raw_spin_lock(&pool_lock); |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 377 | work = (obj_pool_free > debug_objects_pool_size) && obj_cache && |
| 378 | (obj_nr_tofree < ODEBUG_FREE_WORK_MAX); |
Yang Shi | 636e197 | 2018-02-06 07:18:27 +0800 | [diff] [blame] | 379 | obj_pool_used--; |
| 380 | |
| 381 | if (work) { |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 382 | WRITE_ONCE(obj_nr_tofree, obj_nr_tofree + 1); |
Yang Shi | 636e197 | 2018-02-06 07:18:27 +0800 | [diff] [blame] | 383 | hlist_add_head(&obj->node, &obj_to_free); |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 384 | if (lookahead_count) { |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 385 | WRITE_ONCE(obj_nr_tofree, obj_nr_tofree + lookahead_count); |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 386 | obj_pool_used -= lookahead_count; |
| 387 | while (lookahead_count) { |
| 388 | hlist_add_head(&objs[--lookahead_count]->node, |
| 389 | &obj_to_free); |
| 390 | } |
| 391 | } |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 392 | |
| 393 | if ((obj_pool_free > debug_objects_pool_size) && |
| 394 | (obj_nr_tofree < ODEBUG_FREE_WORK_MAX)) { |
| 395 | int i; |
| 396 | |
| 397 | /* |
| 398 | * Free one more batch of objects from obj_pool. |
| 399 | */ |
| 400 | for (i = 0; i < ODEBUG_BATCH_SIZE; i++) { |
| 401 | obj = __alloc_object(&obj_pool); |
| 402 | hlist_add_head(&obj->node, &obj_to_free); |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 403 | WRITE_ONCE(obj_pool_free, obj_pool_free - 1); |
| 404 | WRITE_ONCE(obj_nr_tofree, obj_nr_tofree + 1); |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 405 | } |
| 406 | } |
Yang Shi | 636e197 | 2018-02-06 07:18:27 +0800 | [diff] [blame] | 407 | } else { |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 408 | WRITE_ONCE(obj_pool_free, obj_pool_free + 1); |
Yang Shi | 636e197 | 2018-02-06 07:18:27 +0800 | [diff] [blame] | 409 | hlist_add_head(&obj->node, &obj_pool); |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 410 | if (lookahead_count) { |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 411 | WRITE_ONCE(obj_pool_free, obj_pool_free + lookahead_count); |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 412 | obj_pool_used -= lookahead_count; |
| 413 | while (lookahead_count) { |
| 414 | hlist_add_head(&objs[--lookahead_count]->node, |
| 415 | &obj_pool); |
| 416 | } |
| 417 | } |
Yang Shi | 636e197 | 2018-02-06 07:18:27 +0800 | [diff] [blame] | 418 | } |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 419 | raw_spin_unlock(&pool_lock); |
| 420 | local_irq_restore(flags); |
Yang Shi | 636e197 | 2018-02-06 07:18:27 +0800 | [diff] [blame] | 421 | } |
| 422 | |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 423 | /* |
| 424 | * Put the object back into the pool and schedule work to free objects |
| 425 | * if necessary. |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 426 | */ |
| 427 | static void free_object(struct debug_obj *obj) |
| 428 | { |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 429 | __free_object(obj); |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 430 | if (!READ_ONCE(obj_freeing) && READ_ONCE(obj_nr_tofree)) { |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 431 | WRITE_ONCE(obj_freeing, true); |
| 432 | schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY); |
| 433 | } |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | /* |
| 437 | * We run out of memory. That means we probably have tons of objects |
| 438 | * allocated. |
| 439 | */ |
| 440 | static void debug_objects_oom(void) |
| 441 | { |
| 442 | struct debug_bucket *db = obj_hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 443 | struct hlist_node *tmp; |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 444 | HLIST_HEAD(freelist); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 445 | struct debug_obj *obj; |
| 446 | unsigned long flags; |
| 447 | int i; |
| 448 | |
Fabian Frederick | 719e484 | 2014-06-04 16:06:04 -0700 | [diff] [blame] | 449 | pr_warn("Out of memory. ODEBUG disabled\n"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 450 | |
| 451 | for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) { |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 452 | raw_spin_lock_irqsave(&db->lock, flags); |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 453 | hlist_move_list(&db->list, &freelist); |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 454 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 455 | |
| 456 | /* Now free them */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 457 | hlist_for_each_entry_safe(obj, tmp, &freelist, node) { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 458 | hlist_del(&obj->node); |
| 459 | free_object(obj); |
| 460 | } |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * We use the pfn of the address for the hash. That way we can check |
| 466 | * for freed objects simply by checking the affected bucket. |
| 467 | */ |
| 468 | static struct debug_bucket *get_bucket(unsigned long addr) |
| 469 | { |
| 470 | unsigned long hash; |
| 471 | |
| 472 | hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS); |
| 473 | return &obj_hash[hash]; |
| 474 | } |
| 475 | |
| 476 | static void debug_print_object(struct debug_obj *obj, char *msg) |
| 477 | { |
Stanislaw Gruszka | 9977728 | 2011-03-07 09:58:33 +0100 | [diff] [blame] | 478 | struct debug_obj_descr *descr = obj->descr; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 479 | static int limit; |
| 480 | |
Stanislaw Gruszka | 9977728 | 2011-03-07 09:58:33 +0100 | [diff] [blame] | 481 | if (limit < 5 && descr != descr_test) { |
| 482 | void *hint = descr->debug_hint ? |
| 483 | descr->debug_hint(obj->object) : NULL; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 484 | limit++; |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 485 | WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) " |
Stanislaw Gruszka | 9977728 | 2011-03-07 09:58:33 +0100 | [diff] [blame] | 486 | "object type: %s hint: %pS\n", |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 487 | msg, obj_states[obj->state], obj->astate, |
Stanislaw Gruszka | 9977728 | 2011-03-07 09:58:33 +0100 | [diff] [blame] | 488 | descr->name, hint); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 489 | } |
| 490 | debug_objects_warnings++; |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * Try to repair the damage, so we have a better chance to get useful |
| 495 | * debug output. |
| 496 | */ |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 497 | static bool |
| 498 | debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state), |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 499 | void * addr, enum debug_obj_state state) |
| 500 | { |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 501 | if (fixup && fixup(addr, state)) { |
| 502 | debug_objects_fixups++; |
| 503 | return true; |
| 504 | } |
| 505 | return false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static void debug_object_is_on_stack(void *addr, int onstack) |
| 509 | { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 510 | int is_on_stack; |
| 511 | static int limit; |
| 512 | |
| 513 | if (limit > 4) |
| 514 | return; |
| 515 | |
FUJITA Tomonori | 8b05c7e | 2008-07-23 21:26:53 -0700 | [diff] [blame] | 516 | is_on_stack = object_is_on_stack(addr); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 517 | if (is_on_stack == onstack) |
| 518 | return; |
| 519 | |
| 520 | limit++; |
| 521 | if (is_on_stack) |
Joel Fernandes (Google) | fc91a3c | 2018-07-23 14:25:31 -0700 | [diff] [blame] | 522 | pr_warn("object %p is on stack %p, but NOT annotated.\n", addr, |
| 523 | task_stack_page(current)); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 524 | else |
Joel Fernandes (Google) | fc91a3c | 2018-07-23 14:25:31 -0700 | [diff] [blame] | 525 | pr_warn("object %p is NOT on stack %p, but annotated.\n", addr, |
| 526 | task_stack_page(current)); |
| 527 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 528 | WARN_ON(1); |
| 529 | } |
| 530 | |
| 531 | static void |
| 532 | __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack) |
| 533 | { |
| 534 | enum debug_obj_state state; |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 535 | bool check_stack = false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 536 | struct debug_bucket *db; |
| 537 | struct debug_obj *obj; |
| 538 | unsigned long flags; |
| 539 | |
Vegard Nossum | 50db04dd | 2008-06-15 00:47:36 +0200 | [diff] [blame] | 540 | fill_pool(); |
| 541 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 542 | db = get_bucket((unsigned long) addr); |
| 543 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 544 | raw_spin_lock_irqsave(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 545 | |
| 546 | obj = lookup_object(addr, db); |
| 547 | if (!obj) { |
| 548 | obj = alloc_object(addr, db, descr); |
| 549 | if (!obj) { |
| 550 | debug_objects_enabled = 0; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 551 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 552 | debug_objects_oom(); |
| 553 | return; |
| 554 | } |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 555 | check_stack = true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | switch (obj->state) { |
| 559 | case ODEBUG_STATE_NONE: |
| 560 | case ODEBUG_STATE_INIT: |
| 561 | case ODEBUG_STATE_INACTIVE: |
| 562 | obj->state = ODEBUG_STATE_INIT; |
| 563 | break; |
| 564 | |
| 565 | case ODEBUG_STATE_ACTIVE: |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 566 | state = obj->state; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 567 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 568 | debug_print_object(obj, "init"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 569 | debug_object_fixup(descr->fixup_init, addr, state); |
| 570 | return; |
| 571 | |
| 572 | case ODEBUG_STATE_DESTROYED: |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 573 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 574 | debug_print_object(obj, "init"); |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 575 | return; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 576 | default: |
| 577 | break; |
| 578 | } |
| 579 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 580 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 581 | if (check_stack) |
| 582 | debug_object_is_on_stack(addr, onstack); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /** |
| 586 | * debug_object_init - debug checks when an object is initialized |
| 587 | * @addr: address of the object |
| 588 | * @descr: pointer to an object specific debug description structure |
| 589 | */ |
| 590 | void debug_object_init(void *addr, struct debug_obj_descr *descr) |
| 591 | { |
| 592 | if (!debug_objects_enabled) |
| 593 | return; |
| 594 | |
| 595 | __debug_object_init(addr, descr, 0); |
| 596 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 597 | EXPORT_SYMBOL_GPL(debug_object_init); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 598 | |
| 599 | /** |
| 600 | * debug_object_init_on_stack - debug checks when an object on stack is |
| 601 | * initialized |
| 602 | * @addr: address of the object |
| 603 | * @descr: pointer to an object specific debug description structure |
| 604 | */ |
| 605 | void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr) |
| 606 | { |
| 607 | if (!debug_objects_enabled) |
| 608 | return; |
| 609 | |
| 610 | __debug_object_init(addr, descr, 1); |
| 611 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 612 | EXPORT_SYMBOL_GPL(debug_object_init_on_stack); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 613 | |
| 614 | /** |
| 615 | * debug_object_activate - debug checks when an object is activated |
| 616 | * @addr: address of the object |
| 617 | * @descr: pointer to an object specific debug description structure |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 618 | * Returns 0 for success, -EINVAL for check failed. |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 619 | */ |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 620 | int debug_object_activate(void *addr, struct debug_obj_descr *descr) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 621 | { |
| 622 | enum debug_obj_state state; |
| 623 | struct debug_bucket *db; |
| 624 | struct debug_obj *obj; |
| 625 | unsigned long flags; |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 626 | int ret; |
Stephen Boyd | feac18d | 2011-11-07 19:48:26 -0800 | [diff] [blame] | 627 | struct debug_obj o = { .object = addr, |
| 628 | .state = ODEBUG_STATE_NOTAVAILABLE, |
| 629 | .descr = descr }; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 630 | |
| 631 | if (!debug_objects_enabled) |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 632 | return 0; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 633 | |
| 634 | db = get_bucket((unsigned long) addr); |
| 635 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 636 | raw_spin_lock_irqsave(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 637 | |
| 638 | obj = lookup_object(addr, db); |
| 639 | if (obj) { |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 640 | bool print_object = false; |
| 641 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 642 | switch (obj->state) { |
| 643 | case ODEBUG_STATE_INIT: |
| 644 | case ODEBUG_STATE_INACTIVE: |
| 645 | obj->state = ODEBUG_STATE_ACTIVE; |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 646 | ret = 0; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 647 | break; |
| 648 | |
| 649 | case ODEBUG_STATE_ACTIVE: |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 650 | state = obj->state; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 651 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 652 | debug_print_object(obj, "activate"); |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 653 | ret = debug_object_fixup(descr->fixup_activate, addr, state); |
Du, Changbin | e7a8e78 | 2016-05-19 17:09:23 -0700 | [diff] [blame] | 654 | return ret ? 0 : -EINVAL; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 655 | |
| 656 | case ODEBUG_STATE_DESTROYED: |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 657 | print_object = true; |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 658 | ret = -EINVAL; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 659 | break; |
| 660 | default: |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 661 | ret = 0; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 662 | break; |
| 663 | } |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 664 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 665 | if (print_object) |
| 666 | debug_print_object(obj, "activate"); |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 667 | return ret; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 670 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 671 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 672 | /* |
Du, Changbin | b9fdac7f | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 673 | * We are here when a static object is activated. We |
| 674 | * let the type specific code confirm whether this is |
| 675 | * true or not. if true, we just make sure that the |
| 676 | * static object is tracked in the object tracker. If |
| 677 | * not, this must be a bug, so we try to fix it up. |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 678 | */ |
Du, Changbin | b9fdac7f | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 679 | if (descr->is_static_object && descr->is_static_object(addr)) { |
| 680 | /* track this static object */ |
| 681 | debug_object_init(addr, descr); |
| 682 | debug_object_activate(addr, descr); |
| 683 | } else { |
Stephen Boyd | feac18d | 2011-11-07 19:48:26 -0800 | [diff] [blame] | 684 | debug_print_object(&o, "activate"); |
Du, Changbin | b9fdac7f | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 685 | ret = debug_object_fixup(descr->fixup_activate, addr, |
| 686 | ODEBUG_STATE_NOTAVAILABLE); |
| 687 | return ret ? 0 : -EINVAL; |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 688 | } |
| 689 | return 0; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 690 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 691 | EXPORT_SYMBOL_GPL(debug_object_activate); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 692 | |
| 693 | /** |
| 694 | * debug_object_deactivate - debug checks when an object is deactivated |
| 695 | * @addr: address of the object |
| 696 | * @descr: pointer to an object specific debug description structure |
| 697 | */ |
| 698 | void debug_object_deactivate(void *addr, struct debug_obj_descr *descr) |
| 699 | { |
| 700 | struct debug_bucket *db; |
| 701 | struct debug_obj *obj; |
| 702 | unsigned long flags; |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 703 | bool print_object = false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 704 | |
| 705 | if (!debug_objects_enabled) |
| 706 | return; |
| 707 | |
| 708 | db = get_bucket((unsigned long) addr); |
| 709 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 710 | raw_spin_lock_irqsave(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 711 | |
| 712 | obj = lookup_object(addr, db); |
| 713 | if (obj) { |
| 714 | switch (obj->state) { |
| 715 | case ODEBUG_STATE_INIT: |
| 716 | case ODEBUG_STATE_INACTIVE: |
| 717 | case ODEBUG_STATE_ACTIVE: |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 718 | if (!obj->astate) |
| 719 | obj->state = ODEBUG_STATE_INACTIVE; |
| 720 | else |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 721 | print_object = true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 722 | break; |
| 723 | |
| 724 | case ODEBUG_STATE_DESTROYED: |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 725 | print_object = true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 726 | break; |
| 727 | default: |
| 728 | break; |
| 729 | } |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | raw_spin_unlock_irqrestore(&db->lock, flags); |
| 733 | if (!obj) { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 734 | struct debug_obj o = { .object = addr, |
| 735 | .state = ODEBUG_STATE_NOTAVAILABLE, |
| 736 | .descr = descr }; |
| 737 | |
| 738 | debug_print_object(&o, "deactivate"); |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 739 | } else if (print_object) { |
| 740 | debug_print_object(obj, "deactivate"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 741 | } |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 742 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 743 | EXPORT_SYMBOL_GPL(debug_object_deactivate); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 744 | |
| 745 | /** |
| 746 | * debug_object_destroy - debug checks when an object is destroyed |
| 747 | * @addr: address of the object |
| 748 | * @descr: pointer to an object specific debug description structure |
| 749 | */ |
| 750 | void debug_object_destroy(void *addr, struct debug_obj_descr *descr) |
| 751 | { |
| 752 | enum debug_obj_state state; |
| 753 | struct debug_bucket *db; |
| 754 | struct debug_obj *obj; |
| 755 | unsigned long flags; |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 756 | bool print_object = false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 757 | |
| 758 | if (!debug_objects_enabled) |
| 759 | return; |
| 760 | |
| 761 | db = get_bucket((unsigned long) addr); |
| 762 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 763 | raw_spin_lock_irqsave(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 764 | |
| 765 | obj = lookup_object(addr, db); |
| 766 | if (!obj) |
| 767 | goto out_unlock; |
| 768 | |
| 769 | switch (obj->state) { |
| 770 | case ODEBUG_STATE_NONE: |
| 771 | case ODEBUG_STATE_INIT: |
| 772 | case ODEBUG_STATE_INACTIVE: |
| 773 | obj->state = ODEBUG_STATE_DESTROYED; |
| 774 | break; |
| 775 | case ODEBUG_STATE_ACTIVE: |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 776 | state = obj->state; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 777 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 778 | debug_print_object(obj, "destroy"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 779 | debug_object_fixup(descr->fixup_destroy, addr, state); |
| 780 | return; |
| 781 | |
| 782 | case ODEBUG_STATE_DESTROYED: |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 783 | print_object = true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 784 | break; |
| 785 | default: |
| 786 | break; |
| 787 | } |
| 788 | out_unlock: |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 789 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 790 | if (print_object) |
| 791 | debug_print_object(obj, "destroy"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 792 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 793 | EXPORT_SYMBOL_GPL(debug_object_destroy); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 794 | |
| 795 | /** |
| 796 | * debug_object_free - debug checks when an object is freed |
| 797 | * @addr: address of the object |
| 798 | * @descr: pointer to an object specific debug description structure |
| 799 | */ |
| 800 | void debug_object_free(void *addr, struct debug_obj_descr *descr) |
| 801 | { |
| 802 | enum debug_obj_state state; |
| 803 | struct debug_bucket *db; |
| 804 | struct debug_obj *obj; |
| 805 | unsigned long flags; |
| 806 | |
| 807 | if (!debug_objects_enabled) |
| 808 | return; |
| 809 | |
| 810 | db = get_bucket((unsigned long) addr); |
| 811 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 812 | raw_spin_lock_irqsave(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 813 | |
| 814 | obj = lookup_object(addr, db); |
| 815 | if (!obj) |
| 816 | goto out_unlock; |
| 817 | |
| 818 | switch (obj->state) { |
| 819 | case ODEBUG_STATE_ACTIVE: |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 820 | state = obj->state; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 821 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 822 | debug_print_object(obj, "free"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 823 | debug_object_fixup(descr->fixup_free, addr, state); |
| 824 | return; |
| 825 | default: |
| 826 | hlist_del(&obj->node); |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 827 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 828 | free_object(obj); |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 829 | return; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 830 | } |
| 831 | out_unlock: |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 832 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 833 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 834 | EXPORT_SYMBOL_GPL(debug_object_free); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 835 | |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 836 | /** |
Christine Chan | b84d435 | 2011-11-07 19:48:27 -0800 | [diff] [blame] | 837 | * debug_object_assert_init - debug checks when object should be init-ed |
| 838 | * @addr: address of the object |
| 839 | * @descr: pointer to an object specific debug description structure |
| 840 | */ |
| 841 | void debug_object_assert_init(void *addr, struct debug_obj_descr *descr) |
| 842 | { |
| 843 | struct debug_bucket *db; |
| 844 | struct debug_obj *obj; |
| 845 | unsigned long flags; |
| 846 | |
| 847 | if (!debug_objects_enabled) |
| 848 | return; |
| 849 | |
| 850 | db = get_bucket((unsigned long) addr); |
| 851 | |
| 852 | raw_spin_lock_irqsave(&db->lock, flags); |
| 853 | |
| 854 | obj = lookup_object(addr, db); |
| 855 | if (!obj) { |
| 856 | struct debug_obj o = { .object = addr, |
| 857 | .state = ODEBUG_STATE_NOTAVAILABLE, |
| 858 | .descr = descr }; |
| 859 | |
| 860 | raw_spin_unlock_irqrestore(&db->lock, flags); |
| 861 | /* |
Du, Changbin | b9fdac7f | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 862 | * Maybe the object is static, and we let the type specific |
| 863 | * code confirm. Track this static object if true, else invoke |
| 864 | * fixup. |
Christine Chan | b84d435 | 2011-11-07 19:48:27 -0800 | [diff] [blame] | 865 | */ |
Du, Changbin | b9fdac7f | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 866 | if (descr->is_static_object && descr->is_static_object(addr)) { |
| 867 | /* Track this static object */ |
| 868 | debug_object_init(addr, descr); |
| 869 | } else { |
Christine Chan | b84d435 | 2011-11-07 19:48:27 -0800 | [diff] [blame] | 870 | debug_print_object(&o, "assert_init"); |
Du, Changbin | b9fdac7f | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 871 | debug_object_fixup(descr->fixup_assert_init, addr, |
| 872 | ODEBUG_STATE_NOTAVAILABLE); |
| 873 | } |
Christine Chan | b84d435 | 2011-11-07 19:48:27 -0800 | [diff] [blame] | 874 | return; |
| 875 | } |
| 876 | |
| 877 | raw_spin_unlock_irqrestore(&db->lock, flags); |
| 878 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 879 | EXPORT_SYMBOL_GPL(debug_object_assert_init); |
Christine Chan | b84d435 | 2011-11-07 19:48:27 -0800 | [diff] [blame] | 880 | |
| 881 | /** |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 882 | * debug_object_active_state - debug checks object usage state machine |
| 883 | * @addr: address of the object |
| 884 | * @descr: pointer to an object specific debug description structure |
| 885 | * @expect: expected state |
| 886 | * @next: state to move to if expected state is found |
| 887 | */ |
| 888 | void |
| 889 | debug_object_active_state(void *addr, struct debug_obj_descr *descr, |
| 890 | unsigned int expect, unsigned int next) |
| 891 | { |
| 892 | struct debug_bucket *db; |
| 893 | struct debug_obj *obj; |
| 894 | unsigned long flags; |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 895 | bool print_object = false; |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 896 | |
| 897 | if (!debug_objects_enabled) |
| 898 | return; |
| 899 | |
| 900 | db = get_bucket((unsigned long) addr); |
| 901 | |
| 902 | raw_spin_lock_irqsave(&db->lock, flags); |
| 903 | |
| 904 | obj = lookup_object(addr, db); |
| 905 | if (obj) { |
| 906 | switch (obj->state) { |
| 907 | case ODEBUG_STATE_ACTIVE: |
| 908 | if (obj->astate == expect) |
| 909 | obj->astate = next; |
| 910 | else |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 911 | print_object = true; |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 912 | break; |
| 913 | |
| 914 | default: |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 915 | print_object = true; |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 916 | break; |
| 917 | } |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | raw_spin_unlock_irqrestore(&db->lock, flags); |
| 921 | if (!obj) { |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 922 | struct debug_obj o = { .object = addr, |
| 923 | .state = ODEBUG_STATE_NOTAVAILABLE, |
| 924 | .descr = descr }; |
| 925 | |
| 926 | debug_print_object(&o, "active_state"); |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 927 | } else if (print_object) { |
| 928 | debug_print_object(obj, "active_state"); |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 929 | } |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 930 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 931 | EXPORT_SYMBOL_GPL(debug_object_active_state); |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 932 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 933 | #ifdef CONFIG_DEBUG_OBJECTS_FREE |
| 934 | static void __debug_check_no_obj_freed(const void *address, unsigned long size) |
| 935 | { |
| 936 | unsigned long flags, oaddr, saddr, eaddr, paddr, chunks; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 937 | struct debug_obj_descr *descr; |
| 938 | enum debug_obj_state state; |
| 939 | struct debug_bucket *db; |
Yang Shi | 1ea9b98 | 2018-02-06 07:18:28 +0800 | [diff] [blame] | 940 | struct hlist_node *tmp; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 941 | struct debug_obj *obj; |
Yang Shi | bd9dcd0 | 2018-02-06 07:18:25 +0800 | [diff] [blame] | 942 | int cnt, objs_checked = 0; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 943 | |
| 944 | saddr = (unsigned long) address; |
| 945 | eaddr = saddr + size; |
| 946 | paddr = saddr & ODEBUG_CHUNK_MASK; |
| 947 | chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1)); |
| 948 | chunks >>= ODEBUG_CHUNK_SHIFT; |
| 949 | |
| 950 | for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) { |
| 951 | db = get_bucket(paddr); |
| 952 | |
| 953 | repeat: |
| 954 | cnt = 0; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 955 | raw_spin_lock_irqsave(&db->lock, flags); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 956 | hlist_for_each_entry_safe(obj, tmp, &db->list, node) { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 957 | cnt++; |
| 958 | oaddr = (unsigned long) obj->object; |
| 959 | if (oaddr < saddr || oaddr >= eaddr) |
| 960 | continue; |
| 961 | |
| 962 | switch (obj->state) { |
| 963 | case ODEBUG_STATE_ACTIVE: |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 964 | descr = obj->descr; |
| 965 | state = obj->state; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 966 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Waiman Long | d5f3415 | 2019-05-20 10:14:50 -0400 | [diff] [blame] | 967 | debug_print_object(obj, "free"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 968 | debug_object_fixup(descr->fixup_free, |
| 969 | (void *) oaddr, state); |
| 970 | goto repeat; |
| 971 | default: |
| 972 | hlist_del(&obj->node); |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 973 | __free_object(obj); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 974 | break; |
| 975 | } |
| 976 | } |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 977 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 978 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 979 | if (cnt > debug_objects_maxchain) |
| 980 | debug_objects_maxchain = cnt; |
Yang Shi | bd9dcd0 | 2018-02-06 07:18:25 +0800 | [diff] [blame] | 981 | |
| 982 | objs_checked += cnt; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 983 | } |
Yang Shi | bd9dcd0 | 2018-02-06 07:18:25 +0800 | [diff] [blame] | 984 | |
| 985 | if (objs_checked > debug_objects_maxchecked) |
| 986 | debug_objects_maxchecked = objs_checked; |
Yang Shi | 1ea9b98 | 2018-02-06 07:18:28 +0800 | [diff] [blame] | 987 | |
| 988 | /* Schedule work to actually kmem_cache_free() objects */ |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 989 | if (!READ_ONCE(obj_freeing) && READ_ONCE(obj_nr_tofree)) { |
Waiman Long | a7344a6 | 2019-05-20 10:14:49 -0400 | [diff] [blame] | 990 | WRITE_ONCE(obj_freeing, true); |
| 991 | schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY); |
| 992 | } |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 993 | } |
| 994 | |
| 995 | void debug_check_no_obj_freed(const void *address, unsigned long size) |
| 996 | { |
| 997 | if (debug_objects_enabled) |
| 998 | __debug_check_no_obj_freed(address, size); |
| 999 | } |
| 1000 | #endif |
| 1001 | |
| 1002 | #ifdef CONFIG_DEBUG_FS |
| 1003 | |
| 1004 | static int debug_stats_show(struct seq_file *m, void *v) |
| 1005 | { |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 1006 | int cpu, obj_percpu_free = 0; |
| 1007 | |
| 1008 | for_each_possible_cpu(cpu) |
| 1009 | obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu); |
| 1010 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1011 | seq_printf(m, "max_chain :%d\n", debug_objects_maxchain); |
Yang Shi | bd9dcd0 | 2018-02-06 07:18:25 +0800 | [diff] [blame] | 1012 | seq_printf(m, "max_checked :%d\n", debug_objects_maxchecked); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1013 | seq_printf(m, "warnings :%d\n", debug_objects_warnings); |
| 1014 | seq_printf(m, "fixups :%d\n", debug_objects_fixups); |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 1015 | seq_printf(m, "pool_free :%d\n", READ_ONCE(obj_pool_free) + obj_percpu_free); |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 1016 | seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1017 | seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free); |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 1018 | seq_printf(m, "pool_used :%d\n", obj_pool_used - obj_percpu_free); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1019 | seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used); |
Marco Elver | 35fd7a6 | 2020-01-16 19:55:29 +0100 | [diff] [blame] | 1020 | seq_printf(m, "on_free_list :%d\n", READ_ONCE(obj_nr_tofree)); |
Waiman Long | 0cad93c | 2017-02-07 16:40:30 -0500 | [diff] [blame] | 1021 | seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated); |
| 1022 | seq_printf(m, "objs_freed :%d\n", debug_objects_freed); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1023 | return 0; |
| 1024 | } |
| 1025 | |
| 1026 | static int debug_stats_open(struct inode *inode, struct file *filp) |
| 1027 | { |
| 1028 | return single_open(filp, debug_stats_show, NULL); |
| 1029 | } |
| 1030 | |
| 1031 | static const struct file_operations debug_stats_fops = { |
| 1032 | .open = debug_stats_open, |
| 1033 | .read = seq_read, |
| 1034 | .llseek = seq_lseek, |
| 1035 | .release = single_release, |
| 1036 | }; |
| 1037 | |
| 1038 | static int __init debug_objects_init_debugfs(void) |
| 1039 | { |
Greg Kroah-Hartman | fecb0d9 | 2019-06-12 17:35:13 +0200 | [diff] [blame] | 1040 | struct dentry *dbgdir; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1041 | |
| 1042 | if (!debug_objects_enabled) |
| 1043 | return 0; |
| 1044 | |
| 1045 | dbgdir = debugfs_create_dir("debug_objects", NULL); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1046 | |
Greg Kroah-Hartman | fecb0d9 | 2019-06-12 17:35:13 +0200 | [diff] [blame] | 1047 | debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1048 | |
| 1049 | return 0; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1050 | } |
| 1051 | __initcall(debug_objects_init_debugfs); |
| 1052 | |
| 1053 | #else |
| 1054 | static inline void debug_objects_init_debugfs(void) { } |
| 1055 | #endif |
| 1056 | |
| 1057 | #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST |
| 1058 | |
| 1059 | /* Random data structure for the self test */ |
| 1060 | struct self_test { |
| 1061 | unsigned long dummy1[6]; |
| 1062 | int static_init; |
| 1063 | unsigned long dummy2[3]; |
| 1064 | }; |
| 1065 | |
| 1066 | static __initdata struct debug_obj_descr descr_type_test; |
| 1067 | |
Du, Changbin | b9fdac7f | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 1068 | static bool __init is_static_object(void *addr) |
| 1069 | { |
| 1070 | struct self_test *obj = addr; |
| 1071 | |
| 1072 | return obj->static_init; |
| 1073 | } |
| 1074 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1075 | /* |
| 1076 | * fixup_init is called when: |
| 1077 | * - an active object is initialized |
| 1078 | */ |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1079 | static bool __init fixup_init(void *addr, enum debug_obj_state state) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1080 | { |
| 1081 | struct self_test *obj = addr; |
| 1082 | |
| 1083 | switch (state) { |
| 1084 | case ODEBUG_STATE_ACTIVE: |
| 1085 | debug_object_deactivate(obj, &descr_type_test); |
| 1086 | debug_object_init(obj, &descr_type_test); |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1087 | return true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1088 | default: |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1089 | return false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | /* |
| 1094 | * fixup_activate is called when: |
| 1095 | * - an active object is activated |
Du, Changbin | b9fdac7f | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 1096 | * - an unknown non-static object is activated |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1097 | */ |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1098 | static bool __init fixup_activate(void *addr, enum debug_obj_state state) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1099 | { |
| 1100 | struct self_test *obj = addr; |
| 1101 | |
| 1102 | switch (state) { |
| 1103 | case ODEBUG_STATE_NOTAVAILABLE: |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1104 | return true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1105 | case ODEBUG_STATE_ACTIVE: |
| 1106 | debug_object_deactivate(obj, &descr_type_test); |
| 1107 | debug_object_activate(obj, &descr_type_test); |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1108 | return true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1109 | |
| 1110 | default: |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1111 | return false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * fixup_destroy is called when: |
| 1117 | * - an active object is destroyed |
| 1118 | */ |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1119 | static bool __init fixup_destroy(void *addr, enum debug_obj_state state) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1120 | { |
| 1121 | struct self_test *obj = addr; |
| 1122 | |
| 1123 | switch (state) { |
| 1124 | case ODEBUG_STATE_ACTIVE: |
| 1125 | debug_object_deactivate(obj, &descr_type_test); |
| 1126 | debug_object_destroy(obj, &descr_type_test); |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1127 | return true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1128 | default: |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1129 | return false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1130 | } |
| 1131 | } |
| 1132 | |
| 1133 | /* |
| 1134 | * fixup_free is called when: |
| 1135 | * - an active object is freed |
| 1136 | */ |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1137 | static bool __init fixup_free(void *addr, enum debug_obj_state state) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1138 | { |
| 1139 | struct self_test *obj = addr; |
| 1140 | |
| 1141 | switch (state) { |
| 1142 | case ODEBUG_STATE_ACTIVE: |
| 1143 | debug_object_deactivate(obj, &descr_type_test); |
| 1144 | debug_object_free(obj, &descr_type_test); |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1145 | return true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1146 | default: |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 1147 | return false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1148 | } |
| 1149 | } |
| 1150 | |
Henrik Kretzschmar | 1fb2f77 | 2010-03-26 20:38:35 +0100 | [diff] [blame] | 1151 | static int __init |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1152 | check_results(void *addr, enum debug_obj_state state, int fixups, int warnings) |
| 1153 | { |
| 1154 | struct debug_bucket *db; |
| 1155 | struct debug_obj *obj; |
| 1156 | unsigned long flags; |
| 1157 | int res = -EINVAL; |
| 1158 | |
| 1159 | db = get_bucket((unsigned long) addr); |
| 1160 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 1161 | raw_spin_lock_irqsave(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1162 | |
| 1163 | obj = lookup_object(addr, db); |
| 1164 | if (!obj && state != ODEBUG_STATE_NONE) { |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 1165 | WARN(1, KERN_ERR "ODEBUG: selftest object not found\n"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1166 | goto out; |
| 1167 | } |
| 1168 | if (obj && obj->state != state) { |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 1169 | WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n", |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1170 | obj->state, state); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1171 | goto out; |
| 1172 | } |
| 1173 | if (fixups != debug_objects_fixups) { |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 1174 | WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n", |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1175 | fixups, debug_objects_fixups); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1176 | goto out; |
| 1177 | } |
| 1178 | if (warnings != debug_objects_warnings) { |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 1179 | WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n", |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1180 | warnings, debug_objects_warnings); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1181 | goto out; |
| 1182 | } |
| 1183 | res = 0; |
| 1184 | out: |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 1185 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1186 | if (res) |
| 1187 | debug_objects_enabled = 0; |
| 1188 | return res; |
| 1189 | } |
| 1190 | |
| 1191 | static __initdata struct debug_obj_descr descr_type_test = { |
| 1192 | .name = "selftest", |
Du, Changbin | b9fdac7f | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 1193 | .is_static_object = is_static_object, |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1194 | .fixup_init = fixup_init, |
| 1195 | .fixup_activate = fixup_activate, |
| 1196 | .fixup_destroy = fixup_destroy, |
| 1197 | .fixup_free = fixup_free, |
| 1198 | }; |
| 1199 | |
| 1200 | static __initdata struct self_test obj = { .static_init = 0 }; |
| 1201 | |
| 1202 | static void __init debug_objects_selftest(void) |
| 1203 | { |
| 1204 | int fixups, oldfixups, warnings, oldwarnings; |
| 1205 | unsigned long flags; |
| 1206 | |
| 1207 | local_irq_save(flags); |
| 1208 | |
| 1209 | fixups = oldfixups = debug_objects_fixups; |
| 1210 | warnings = oldwarnings = debug_objects_warnings; |
| 1211 | descr_test = &descr_type_test; |
| 1212 | |
| 1213 | debug_object_init(&obj, &descr_type_test); |
| 1214 | if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings)) |
| 1215 | goto out; |
| 1216 | debug_object_activate(&obj, &descr_type_test); |
| 1217 | if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) |
| 1218 | goto out; |
| 1219 | debug_object_activate(&obj, &descr_type_test); |
| 1220 | if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings)) |
| 1221 | goto out; |
| 1222 | debug_object_deactivate(&obj, &descr_type_test); |
| 1223 | if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings)) |
| 1224 | goto out; |
| 1225 | debug_object_destroy(&obj, &descr_type_test); |
| 1226 | if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings)) |
| 1227 | goto out; |
| 1228 | debug_object_init(&obj, &descr_type_test); |
| 1229 | if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) |
| 1230 | goto out; |
| 1231 | debug_object_activate(&obj, &descr_type_test); |
| 1232 | if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) |
| 1233 | goto out; |
| 1234 | debug_object_deactivate(&obj, &descr_type_test); |
| 1235 | if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) |
| 1236 | goto out; |
| 1237 | debug_object_free(&obj, &descr_type_test); |
| 1238 | if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings)) |
| 1239 | goto out; |
| 1240 | |
| 1241 | obj.static_init = 1; |
| 1242 | debug_object_activate(&obj, &descr_type_test); |
Stephen Boyd | 9f78ff0 | 2012-03-05 14:59:17 -0800 | [diff] [blame] | 1243 | if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1244 | goto out; |
| 1245 | debug_object_init(&obj, &descr_type_test); |
| 1246 | if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings)) |
| 1247 | goto out; |
| 1248 | debug_object_free(&obj, &descr_type_test); |
| 1249 | if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings)) |
| 1250 | goto out; |
| 1251 | |
| 1252 | #ifdef CONFIG_DEBUG_OBJECTS_FREE |
| 1253 | debug_object_init(&obj, &descr_type_test); |
| 1254 | if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings)) |
| 1255 | goto out; |
| 1256 | debug_object_activate(&obj, &descr_type_test); |
| 1257 | if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) |
| 1258 | goto out; |
| 1259 | __debug_check_no_obj_freed(&obj, sizeof(obj)); |
| 1260 | if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings)) |
| 1261 | goto out; |
| 1262 | #endif |
Fabian Frederick | 719e484 | 2014-06-04 16:06:04 -0700 | [diff] [blame] | 1263 | pr_info("selftest passed\n"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1264 | |
| 1265 | out: |
| 1266 | debug_objects_fixups = oldfixups; |
| 1267 | debug_objects_warnings = oldwarnings; |
| 1268 | descr_test = NULL; |
| 1269 | |
| 1270 | local_irq_restore(flags); |
| 1271 | } |
| 1272 | #else |
| 1273 | static inline void debug_objects_selftest(void) { } |
| 1274 | #endif |
| 1275 | |
| 1276 | /* |
| 1277 | * Called during early boot to initialize the hash buckets and link |
| 1278 | * the static object pool objects into the poll list. After this call |
| 1279 | * the object tracker is fully operational. |
| 1280 | */ |
| 1281 | void __init debug_objects_early_init(void) |
| 1282 | { |
| 1283 | int i; |
| 1284 | |
| 1285 | for (i = 0; i < ODEBUG_HASH_SIZE; i++) |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 1286 | raw_spin_lock_init(&obj_hash[i].lock); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1287 | |
| 1288 | for (i = 0; i < ODEBUG_POOL_SIZE; i++) |
| 1289 | hlist_add_head(&obj_static_pool[i].node, &obj_pool); |
| 1290 | } |
| 1291 | |
| 1292 | /* |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1293 | * Convert the statically allocated objects to dynamic ones: |
| 1294 | */ |
Henrik Kretzschmar | 1fb2f77 | 2010-03-26 20:38:35 +0100 | [diff] [blame] | 1295 | static int __init debug_objects_replace_static_objects(void) |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1296 | { |
| 1297 | struct debug_bucket *db = obj_hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1298 | struct hlist_node *tmp; |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1299 | struct debug_obj *obj, *new; |
| 1300 | HLIST_HEAD(objects); |
| 1301 | int i, cnt = 0; |
| 1302 | |
| 1303 | for (i = 0; i < ODEBUG_POOL_SIZE; i++) { |
| 1304 | obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL); |
| 1305 | if (!obj) |
| 1306 | goto free; |
| 1307 | hlist_add_head(&obj->node, &objects); |
| 1308 | } |
| 1309 | |
| 1310 | /* |
Qian Cai | a9ee3a6 | 2018-12-28 00:32:32 -0800 | [diff] [blame] | 1311 | * debug_objects_mem_init() is now called early that only one CPU is up |
| 1312 | * and interrupts have been disabled, so it is safe to replace the |
| 1313 | * active object references. |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1314 | */ |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1315 | |
| 1316 | /* Remove the statically allocated objects from the pool */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1317 | hlist_for_each_entry_safe(obj, tmp, &obj_pool, node) |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1318 | hlist_del(&obj->node); |
| 1319 | /* Move the allocated objects to the pool */ |
| 1320 | hlist_move_list(&objects, &obj_pool); |
| 1321 | |
| 1322 | /* Replace the active object references */ |
| 1323 | for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) { |
| 1324 | hlist_move_list(&db->list, &objects); |
| 1325 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1326 | hlist_for_each_entry(obj, &objects, node) { |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1327 | new = hlist_entry(obj_pool.first, typeof(*obj), node); |
| 1328 | hlist_del(&new->node); |
| 1329 | /* copy object data */ |
| 1330 | *new = *obj; |
| 1331 | hlist_add_head(&new->node, &db->list); |
| 1332 | cnt++; |
| 1333 | } |
| 1334 | } |
| 1335 | |
Fabian Frederick | c0f35cc | 2014-06-04 16:06:05 -0700 | [diff] [blame] | 1336 | pr_debug("%d of %d active objects replaced\n", |
| 1337 | cnt, obj_pool_used); |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1338 | return 0; |
| 1339 | free: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1340 | hlist_for_each_entry_safe(obj, tmp, &objects, node) { |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1341 | hlist_del(&obj->node); |
| 1342 | kmem_cache_free(obj_cache, obj); |
| 1343 | } |
| 1344 | return -ENOMEM; |
| 1345 | } |
| 1346 | |
| 1347 | /* |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1348 | * Called after the kmem_caches are functional to setup a dedicated |
| 1349 | * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag |
| 1350 | * prevents that the debug code is called on kmem_cache_free() for the |
| 1351 | * debug tracker objects to avoid recursive calls. |
| 1352 | */ |
| 1353 | void __init debug_objects_mem_init(void) |
| 1354 | { |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 1355 | int cpu, extras; |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 1356 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1357 | if (!debug_objects_enabled) |
| 1358 | return; |
| 1359 | |
Waiman Long | d86998b | 2019-05-20 10:14:46 -0400 | [diff] [blame] | 1360 | /* |
| 1361 | * Initialize the percpu object pools |
| 1362 | * |
| 1363 | * Initialization is not strictly necessary, but was done for |
| 1364 | * completeness. |
| 1365 | */ |
| 1366 | for_each_possible_cpu(cpu) |
| 1367 | INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool.free_objs, cpu)); |
| 1368 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1369 | obj_cache = kmem_cache_create("debug_objects_cache", |
| 1370 | sizeof (struct debug_obj), 0, |
Qian Cai | 8de456c | 2018-11-30 14:09:48 -0800 | [diff] [blame] | 1371 | SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE, |
| 1372 | NULL); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1373 | |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1374 | if (!obj_cache || debug_objects_replace_static_objects()) { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1375 | debug_objects_enabled = 0; |
Zhong Jiang | 3ff4f80 | 2018-08-01 00:24:58 +0800 | [diff] [blame] | 1376 | kmem_cache_destroy(obj_cache); |
Fabian Frederick | 719e484 | 2014-06-04 16:06:04 -0700 | [diff] [blame] | 1377 | pr_warn("out of memory.\n"); |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1378 | } else |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1379 | debug_objects_selftest(); |
Waiman Long | 634d61f | 2019-05-20 10:14:47 -0400 | [diff] [blame] | 1380 | |
| 1381 | /* |
| 1382 | * Increase the thresholds for allocating and freeing objects |
| 1383 | * according to the number of possible CPUs available in the system. |
| 1384 | */ |
| 1385 | extras = num_possible_cpus() * ODEBUG_BATCH_SIZE; |
| 1386 | debug_objects_pool_size += extras; |
| 1387 | debug_objects_pool_min_level += extras; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1388 | } |