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> |
| 21 | |
| 22 | #define ODEBUG_HASH_BITS 14 |
| 23 | #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS) |
| 24 | |
Christian Borntraeger | 0b6ec8c | 2016-01-27 15:37:58 +0100 | [diff] [blame] | 25 | #define ODEBUG_POOL_SIZE 1024 |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 26 | #define ODEBUG_POOL_MIN_LEVEL 256 |
| 27 | |
| 28 | #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT |
| 29 | #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT) |
| 30 | #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1)) |
| 31 | |
| 32 | struct debug_bucket { |
| 33 | struct hlist_head list; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 34 | raw_spinlock_t lock; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE]; |
| 38 | |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 39 | static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 40 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 41 | static DEFINE_RAW_SPINLOCK(pool_lock); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 42 | |
| 43 | static HLIST_HEAD(obj_pool); |
| 44 | |
| 45 | static int obj_pool_min_free = ODEBUG_POOL_SIZE; |
| 46 | static int obj_pool_free = ODEBUG_POOL_SIZE; |
| 47 | static int obj_pool_used; |
| 48 | static int obj_pool_max_used; |
| 49 | static struct kmem_cache *obj_cache; |
| 50 | |
| 51 | static int debug_objects_maxchain __read_mostly; |
| 52 | static int debug_objects_fixups __read_mostly; |
| 53 | static int debug_objects_warnings __read_mostly; |
Ingo Molnar | 3ae7020 | 2008-11-26 10:02:00 +0100 | [diff] [blame] | 54 | static int debug_objects_enabled __read_mostly |
| 55 | = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT; |
Waiman Long | 97dd552 | 2017-01-05 15:17:04 -0500 | [diff] [blame] | 56 | static int debug_objects_pool_size __read_mostly |
| 57 | = ODEBUG_POOL_SIZE; |
| 58 | static int debug_objects_pool_min_level __read_mostly |
| 59 | = ODEBUG_POOL_MIN_LEVEL; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 60 | static struct debug_obj_descr *descr_test __read_mostly; |
| 61 | |
Waiman Long | c4b73aa | 2017-01-05 15:17:03 -0500 | [diff] [blame] | 62 | /* |
Waiman Long | 0cad93c | 2017-02-07 16:40:30 -0500 | [diff] [blame] | 63 | * Track numbers of kmem_cache_alloc()/free() calls done. |
Waiman Long | c4b73aa | 2017-01-05 15:17:03 -0500 | [diff] [blame] | 64 | */ |
Waiman Long | 0cad93c | 2017-02-07 16:40:30 -0500 | [diff] [blame] | 65 | static int debug_objects_allocated; |
Waiman Long | c4b73aa | 2017-01-05 15:17:03 -0500 | [diff] [blame] | 66 | static int debug_objects_freed; |
| 67 | |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 68 | static void free_obj_work(struct work_struct *work); |
| 69 | static DECLARE_WORK(debug_obj_work, free_obj_work); |
| 70 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 71 | static int __init enable_object_debug(char *str) |
| 72 | { |
| 73 | debug_objects_enabled = 1; |
| 74 | return 0; |
| 75 | } |
Kyle McMartin | 3e8ebb5 | 2009-03-01 20:41:41 -0500 | [diff] [blame] | 76 | |
| 77 | static int __init disable_object_debug(char *str) |
| 78 | { |
| 79 | debug_objects_enabled = 0; |
| 80 | return 0; |
| 81 | } |
| 82 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 83 | early_param("debug_objects", enable_object_debug); |
Kyle McMartin | 3e8ebb5 | 2009-03-01 20:41:41 -0500 | [diff] [blame] | 84 | early_param("no_debug_objects", disable_object_debug); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 85 | |
| 86 | static const char *obj_states[ODEBUG_STATE_MAX] = { |
| 87 | [ODEBUG_STATE_NONE] = "none", |
| 88 | [ODEBUG_STATE_INIT] = "initialized", |
| 89 | [ODEBUG_STATE_INACTIVE] = "inactive", |
| 90 | [ODEBUG_STATE_ACTIVE] = "active", |
| 91 | [ODEBUG_STATE_DESTROYED] = "destroyed", |
| 92 | [ODEBUG_STATE_NOTAVAILABLE] = "not available", |
| 93 | }; |
| 94 | |
Thomas Gleixner | 1fda107 | 2012-04-11 11:52:18 +0200 | [diff] [blame] | 95 | static void fill_pool(void) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 96 | { |
| 97 | gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN; |
| 98 | struct debug_obj *new; |
Vegard Nossum | 50db04dd | 2008-06-15 00:47:36 +0200 | [diff] [blame] | 99 | unsigned long flags; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 100 | |
Waiman Long | 97dd552 | 2017-01-05 15:17:04 -0500 | [diff] [blame] | 101 | if (likely(obj_pool_free >= debug_objects_pool_min_level)) |
Thomas Gleixner | 1fda107 | 2012-04-11 11:52:18 +0200 | [diff] [blame] | 102 | return; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 103 | |
| 104 | if (unlikely(!obj_cache)) |
Thomas Gleixner | 1fda107 | 2012-04-11 11:52:18 +0200 | [diff] [blame] | 105 | return; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 106 | |
Waiman Long | 97dd552 | 2017-01-05 15:17:04 -0500 | [diff] [blame] | 107 | while (obj_pool_free < debug_objects_pool_min_level) { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 108 | |
| 109 | new = kmem_cache_zalloc(obj_cache, gfp); |
| 110 | if (!new) |
Dan Carpenter | 3340808 | 2012-04-18 14:28:10 +0300 | [diff] [blame] | 111 | return; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 112 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 113 | raw_spin_lock_irqsave(&pool_lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 114 | hlist_add_head(&new->node, &obj_pool); |
Waiman Long | 0cad93c | 2017-02-07 16:40:30 -0500 | [diff] [blame] | 115 | debug_objects_allocated++; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 116 | obj_pool_free++; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 117 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 118 | } |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Lookup an object in the hash bucket. |
| 123 | */ |
| 124 | static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b) |
| 125 | { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 126 | struct debug_obj *obj; |
| 127 | int cnt = 0; |
| 128 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 129 | hlist_for_each_entry(obj, &b->list, node) { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 130 | cnt++; |
| 131 | if (obj->object == addr) |
| 132 | return obj; |
| 133 | } |
| 134 | if (cnt > debug_objects_maxchain) |
| 135 | debug_objects_maxchain = cnt; |
| 136 | |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | /* |
Vegard Nossum | 50db04dd | 2008-06-15 00:47:36 +0200 | [diff] [blame] | 141 | * 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] | 142 | * Must be called with interrupts disabled. |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 143 | */ |
| 144 | static struct debug_obj * |
| 145 | alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr) |
| 146 | { |
| 147 | struct debug_obj *obj = NULL; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 148 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 149 | raw_spin_lock(&pool_lock); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 150 | if (obj_pool.first) { |
| 151 | obj = hlist_entry(obj_pool.first, typeof(*obj), node); |
| 152 | |
| 153 | obj->object = addr; |
| 154 | obj->descr = descr; |
| 155 | obj->state = ODEBUG_STATE_NONE; |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 156 | obj->astate = 0; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 157 | hlist_del(&obj->node); |
| 158 | |
| 159 | hlist_add_head(&obj->node, &b->list); |
| 160 | |
| 161 | obj_pool_used++; |
| 162 | if (obj_pool_used > obj_pool_max_used) |
| 163 | obj_pool_max_used = obj_pool_used; |
| 164 | |
| 165 | obj_pool_free--; |
| 166 | if (obj_pool_free < obj_pool_min_free) |
| 167 | obj_pool_min_free = obj_pool_free; |
| 168 | } |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 169 | raw_spin_unlock(&pool_lock); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 170 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 171 | return obj; |
| 172 | } |
| 173 | |
| 174 | /* |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 175 | * workqueue function to free objects. |
Waiman Long | 858274b | 2017-01-05 15:17:05 -0500 | [diff] [blame] | 176 | * |
| 177 | * To reduce contention on the global pool_lock, the actual freeing of |
| 178 | * debug objects will be delayed if the pool_lock is busy. We also free |
| 179 | * the objects in a batch of 4 for each lock/unlock cycle. |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 180 | */ |
Waiman Long | 858274b | 2017-01-05 15:17:05 -0500 | [diff] [blame] | 181 | #define ODEBUG_FREE_BATCH 4 |
| 182 | |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 183 | static void free_obj_work(struct work_struct *work) |
| 184 | { |
Waiman Long | 858274b | 2017-01-05 15:17:05 -0500 | [diff] [blame] | 185 | struct debug_obj *objs[ODEBUG_FREE_BATCH]; |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 186 | unsigned long flags; |
Waiman Long | 858274b | 2017-01-05 15:17:05 -0500 | [diff] [blame] | 187 | int i; |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 188 | |
Waiman Long | 858274b | 2017-01-05 15:17:05 -0500 | [diff] [blame] | 189 | if (!raw_spin_trylock_irqsave(&pool_lock, flags)) |
| 190 | return; |
| 191 | while (obj_pool_free >= debug_objects_pool_size + ODEBUG_FREE_BATCH) { |
| 192 | for (i = 0; i < ODEBUG_FREE_BATCH; i++) { |
| 193 | objs[i] = hlist_entry(obj_pool.first, |
| 194 | typeof(*objs[0]), node); |
| 195 | hlist_del(&objs[i]->node); |
| 196 | } |
| 197 | |
| 198 | obj_pool_free -= ODEBUG_FREE_BATCH; |
| 199 | debug_objects_freed += ODEBUG_FREE_BATCH; |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 200 | /* |
| 201 | * We release pool_lock across kmem_cache_free() to |
| 202 | * avoid contention on pool_lock. |
| 203 | */ |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 204 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
Waiman Long | 858274b | 2017-01-05 15:17:05 -0500 | [diff] [blame] | 205 | for (i = 0; i < ODEBUG_FREE_BATCH; i++) |
| 206 | kmem_cache_free(obj_cache, objs[i]); |
| 207 | if (!raw_spin_trylock_irqsave(&pool_lock, flags)) |
| 208 | return; |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 209 | } |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 210 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Put the object back into the pool and schedule work to free objects |
| 215 | * if necessary. |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 216 | */ |
| 217 | static void free_object(struct debug_obj *obj) |
| 218 | { |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 219 | unsigned long flags; |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 220 | int sched = 0; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 221 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 222 | raw_spin_lock_irqsave(&pool_lock, flags); |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 223 | /* |
| 224 | * schedule work when the pool is filled and the cache is |
| 225 | * initialized: |
| 226 | */ |
Waiman Long | 97dd552 | 2017-01-05 15:17:04 -0500 | [diff] [blame] | 227 | if (obj_pool_free > debug_objects_pool_size && obj_cache) |
Tejun Heo | 7092dff | 2016-09-16 15:49:34 -0400 | [diff] [blame] | 228 | sched = 1; |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 229 | hlist_add_head(&obj->node, &obj_pool); |
| 230 | obj_pool_free++; |
| 231 | obj_pool_used--; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 232 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
Thomas Gleixner | 337fff8 | 2009-03-16 10:04:53 +0100 | [diff] [blame] | 233 | if (sched) |
| 234 | schedule_work(&debug_obj_work); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* |
| 238 | * We run out of memory. That means we probably have tons of objects |
| 239 | * allocated. |
| 240 | */ |
| 241 | static void debug_objects_oom(void) |
| 242 | { |
| 243 | struct debug_bucket *db = obj_hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 244 | struct hlist_node *tmp; |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 245 | HLIST_HEAD(freelist); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 246 | struct debug_obj *obj; |
| 247 | unsigned long flags; |
| 248 | int i; |
| 249 | |
Fabian Frederick | 719e484 | 2014-06-04 16:06:04 -0700 | [diff] [blame] | 250 | pr_warn("Out of memory. ODEBUG disabled\n"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 251 | |
| 252 | for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) { |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 253 | raw_spin_lock_irqsave(&db->lock, flags); |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 254 | hlist_move_list(&db->list, &freelist); |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 255 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 256 | |
| 257 | /* Now free them */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 258 | hlist_for_each_entry_safe(obj, tmp, &freelist, node) { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 259 | hlist_del(&obj->node); |
| 260 | free_object(obj); |
| 261 | } |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * We use the pfn of the address for the hash. That way we can check |
| 267 | * for freed objects simply by checking the affected bucket. |
| 268 | */ |
| 269 | static struct debug_bucket *get_bucket(unsigned long addr) |
| 270 | { |
| 271 | unsigned long hash; |
| 272 | |
| 273 | hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS); |
| 274 | return &obj_hash[hash]; |
| 275 | } |
| 276 | |
| 277 | static void debug_print_object(struct debug_obj *obj, char *msg) |
| 278 | { |
Stanislaw Gruszka | 9977728 | 2011-03-07 09:58:33 +0100 | [diff] [blame] | 279 | struct debug_obj_descr *descr = obj->descr; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 280 | static int limit; |
| 281 | |
Stanislaw Gruszka | 9977728 | 2011-03-07 09:58:33 +0100 | [diff] [blame] | 282 | if (limit < 5 && descr != descr_test) { |
| 283 | void *hint = descr->debug_hint ? |
| 284 | descr->debug_hint(obj->object) : NULL; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 285 | limit++; |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 286 | WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) " |
Stanislaw Gruszka | 9977728 | 2011-03-07 09:58:33 +0100 | [diff] [blame] | 287 | "object type: %s hint: %pS\n", |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 288 | msg, obj_states[obj->state], obj->astate, |
Stanislaw Gruszka | 9977728 | 2011-03-07 09:58:33 +0100 | [diff] [blame] | 289 | descr->name, hint); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 290 | } |
| 291 | debug_objects_warnings++; |
| 292 | } |
| 293 | |
| 294 | /* |
| 295 | * Try to repair the damage, so we have a better chance to get useful |
| 296 | * debug output. |
| 297 | */ |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 298 | static bool |
| 299 | debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state), |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 300 | void * addr, enum debug_obj_state state) |
| 301 | { |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 302 | if (fixup && fixup(addr, state)) { |
| 303 | debug_objects_fixups++; |
| 304 | return true; |
| 305 | } |
| 306 | return false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static void debug_object_is_on_stack(void *addr, int onstack) |
| 310 | { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 311 | int is_on_stack; |
| 312 | static int limit; |
| 313 | |
| 314 | if (limit > 4) |
| 315 | return; |
| 316 | |
FUJITA Tomonori | 8b05c7e | 2008-07-23 21:26:53 -0700 | [diff] [blame] | 317 | is_on_stack = object_is_on_stack(addr); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 318 | if (is_on_stack == onstack) |
| 319 | return; |
| 320 | |
| 321 | limit++; |
| 322 | if (is_on_stack) |
Fabian Frederick | 719e484 | 2014-06-04 16:06:04 -0700 | [diff] [blame] | 323 | pr_warn("object is on stack, but not annotated\n"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 324 | else |
Fabian Frederick | 719e484 | 2014-06-04 16:06:04 -0700 | [diff] [blame] | 325 | pr_warn("object is not on stack, but annotated\n"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 326 | WARN_ON(1); |
| 327 | } |
| 328 | |
| 329 | static void |
| 330 | __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack) |
| 331 | { |
| 332 | enum debug_obj_state state; |
| 333 | struct debug_bucket *db; |
| 334 | struct debug_obj *obj; |
| 335 | unsigned long flags; |
| 336 | |
Vegard Nossum | 50db04dd | 2008-06-15 00:47:36 +0200 | [diff] [blame] | 337 | fill_pool(); |
| 338 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 339 | db = get_bucket((unsigned long) addr); |
| 340 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 341 | raw_spin_lock_irqsave(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 342 | |
| 343 | obj = lookup_object(addr, db); |
| 344 | if (!obj) { |
| 345 | obj = alloc_object(addr, db, descr); |
| 346 | if (!obj) { |
| 347 | debug_objects_enabled = 0; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 348 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 349 | debug_objects_oom(); |
| 350 | return; |
| 351 | } |
| 352 | debug_object_is_on_stack(addr, onstack); |
| 353 | } |
| 354 | |
| 355 | switch (obj->state) { |
| 356 | case ODEBUG_STATE_NONE: |
| 357 | case ODEBUG_STATE_INIT: |
| 358 | case ODEBUG_STATE_INACTIVE: |
| 359 | obj->state = ODEBUG_STATE_INIT; |
| 360 | break; |
| 361 | |
| 362 | case ODEBUG_STATE_ACTIVE: |
| 363 | debug_print_object(obj, "init"); |
| 364 | state = obj->state; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 365 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 366 | debug_object_fixup(descr->fixup_init, addr, state); |
| 367 | return; |
| 368 | |
| 369 | case ODEBUG_STATE_DESTROYED: |
| 370 | debug_print_object(obj, "init"); |
| 371 | break; |
| 372 | default: |
| 373 | break; |
| 374 | } |
| 375 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 376 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /** |
| 380 | * debug_object_init - debug checks when an object is initialized |
| 381 | * @addr: address of the object |
| 382 | * @descr: pointer to an object specific debug description structure |
| 383 | */ |
| 384 | void debug_object_init(void *addr, struct debug_obj_descr *descr) |
| 385 | { |
| 386 | if (!debug_objects_enabled) |
| 387 | return; |
| 388 | |
| 389 | __debug_object_init(addr, descr, 0); |
| 390 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 391 | EXPORT_SYMBOL_GPL(debug_object_init); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 392 | |
| 393 | /** |
| 394 | * debug_object_init_on_stack - debug checks when an object on stack is |
| 395 | * initialized |
| 396 | * @addr: address of the object |
| 397 | * @descr: pointer to an object specific debug description structure |
| 398 | */ |
| 399 | void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr) |
| 400 | { |
| 401 | if (!debug_objects_enabled) |
| 402 | return; |
| 403 | |
| 404 | __debug_object_init(addr, descr, 1); |
| 405 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 406 | EXPORT_SYMBOL_GPL(debug_object_init_on_stack); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 407 | |
| 408 | /** |
| 409 | * debug_object_activate - debug checks when an object is activated |
| 410 | * @addr: address of the object |
| 411 | * @descr: pointer to an object specific debug description structure |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 412 | * Returns 0 for success, -EINVAL for check failed. |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 413 | */ |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 414 | int debug_object_activate(void *addr, struct debug_obj_descr *descr) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 415 | { |
| 416 | enum debug_obj_state state; |
| 417 | struct debug_bucket *db; |
| 418 | struct debug_obj *obj; |
| 419 | unsigned long flags; |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 420 | int ret; |
Stephen Boyd | feac18d | 2011-11-07 19:48:26 -0800 | [diff] [blame] | 421 | struct debug_obj o = { .object = addr, |
| 422 | .state = ODEBUG_STATE_NOTAVAILABLE, |
| 423 | .descr = descr }; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 424 | |
| 425 | if (!debug_objects_enabled) |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 426 | return 0; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 427 | |
| 428 | db = get_bucket((unsigned long) addr); |
| 429 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 430 | raw_spin_lock_irqsave(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 431 | |
| 432 | obj = lookup_object(addr, db); |
| 433 | if (obj) { |
| 434 | switch (obj->state) { |
| 435 | case ODEBUG_STATE_INIT: |
| 436 | case ODEBUG_STATE_INACTIVE: |
| 437 | obj->state = ODEBUG_STATE_ACTIVE; |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 438 | ret = 0; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 439 | break; |
| 440 | |
| 441 | case ODEBUG_STATE_ACTIVE: |
| 442 | debug_print_object(obj, "activate"); |
| 443 | state = obj->state; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 444 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 445 | ret = debug_object_fixup(descr->fixup_activate, addr, state); |
Du, Changbin | e7a8e78 | 2016-05-19 17:09:23 -0700 | [diff] [blame] | 446 | return ret ? 0 : -EINVAL; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 447 | |
| 448 | case ODEBUG_STATE_DESTROYED: |
| 449 | debug_print_object(obj, "activate"); |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 450 | ret = -EINVAL; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 451 | break; |
| 452 | default: |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 453 | ret = 0; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 454 | break; |
| 455 | } |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 456 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 457 | return ret; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 460 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 461 | /* |
Du, Changbin | b9fdac7 | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 462 | * We are here when a static object is activated. We |
| 463 | * let the type specific code confirm whether this is |
| 464 | * true or not. if true, we just make sure that the |
| 465 | * static object is tracked in the object tracker. If |
| 466 | * 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] | 467 | */ |
Du, Changbin | b9fdac7 | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 468 | if (descr->is_static_object && descr->is_static_object(addr)) { |
| 469 | /* track this static object */ |
| 470 | debug_object_init(addr, descr); |
| 471 | debug_object_activate(addr, descr); |
| 472 | } else { |
Stephen Boyd | feac18d | 2011-11-07 19:48:26 -0800 | [diff] [blame] | 473 | debug_print_object(&o, "activate"); |
Du, Changbin | b9fdac7 | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 474 | ret = debug_object_fixup(descr->fixup_activate, addr, |
| 475 | ODEBUG_STATE_NOTAVAILABLE); |
| 476 | return ret ? 0 : -EINVAL; |
Paul E. McKenney | b778ae2 | 2013-04-23 12:51:11 -0700 | [diff] [blame] | 477 | } |
| 478 | return 0; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 479 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 480 | EXPORT_SYMBOL_GPL(debug_object_activate); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 481 | |
| 482 | /** |
| 483 | * debug_object_deactivate - debug checks when an object is deactivated |
| 484 | * @addr: address of the object |
| 485 | * @descr: pointer to an object specific debug description structure |
| 486 | */ |
| 487 | void debug_object_deactivate(void *addr, struct debug_obj_descr *descr) |
| 488 | { |
| 489 | struct debug_bucket *db; |
| 490 | struct debug_obj *obj; |
| 491 | unsigned long flags; |
| 492 | |
| 493 | if (!debug_objects_enabled) |
| 494 | return; |
| 495 | |
| 496 | db = get_bucket((unsigned long) addr); |
| 497 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 498 | raw_spin_lock_irqsave(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 499 | |
| 500 | obj = lookup_object(addr, db); |
| 501 | if (obj) { |
| 502 | switch (obj->state) { |
| 503 | case ODEBUG_STATE_INIT: |
| 504 | case ODEBUG_STATE_INACTIVE: |
| 505 | case ODEBUG_STATE_ACTIVE: |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 506 | if (!obj->astate) |
| 507 | obj->state = ODEBUG_STATE_INACTIVE; |
| 508 | else |
| 509 | debug_print_object(obj, "deactivate"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 510 | break; |
| 511 | |
| 512 | case ODEBUG_STATE_DESTROYED: |
| 513 | debug_print_object(obj, "deactivate"); |
| 514 | break; |
| 515 | default: |
| 516 | break; |
| 517 | } |
| 518 | } else { |
| 519 | struct debug_obj o = { .object = addr, |
| 520 | .state = ODEBUG_STATE_NOTAVAILABLE, |
| 521 | .descr = descr }; |
| 522 | |
| 523 | debug_print_object(&o, "deactivate"); |
| 524 | } |
| 525 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 526 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 527 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 528 | EXPORT_SYMBOL_GPL(debug_object_deactivate); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 529 | |
| 530 | /** |
| 531 | * debug_object_destroy - debug checks when an object is destroyed |
| 532 | * @addr: address of the object |
| 533 | * @descr: pointer to an object specific debug description structure |
| 534 | */ |
| 535 | void debug_object_destroy(void *addr, struct debug_obj_descr *descr) |
| 536 | { |
| 537 | enum debug_obj_state state; |
| 538 | struct debug_bucket *db; |
| 539 | struct debug_obj *obj; |
| 540 | unsigned long flags; |
| 541 | |
| 542 | if (!debug_objects_enabled) |
| 543 | return; |
| 544 | |
| 545 | db = get_bucket((unsigned long) addr); |
| 546 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 547 | raw_spin_lock_irqsave(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 548 | |
| 549 | obj = lookup_object(addr, db); |
| 550 | if (!obj) |
| 551 | goto out_unlock; |
| 552 | |
| 553 | switch (obj->state) { |
| 554 | case ODEBUG_STATE_NONE: |
| 555 | case ODEBUG_STATE_INIT: |
| 556 | case ODEBUG_STATE_INACTIVE: |
| 557 | obj->state = ODEBUG_STATE_DESTROYED; |
| 558 | break; |
| 559 | case ODEBUG_STATE_ACTIVE: |
| 560 | debug_print_object(obj, "destroy"); |
| 561 | state = obj->state; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 562 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 563 | debug_object_fixup(descr->fixup_destroy, addr, state); |
| 564 | return; |
| 565 | |
| 566 | case ODEBUG_STATE_DESTROYED: |
| 567 | debug_print_object(obj, "destroy"); |
| 568 | break; |
| 569 | default: |
| 570 | break; |
| 571 | } |
| 572 | out_unlock: |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 573 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 574 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 575 | EXPORT_SYMBOL_GPL(debug_object_destroy); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 576 | |
| 577 | /** |
| 578 | * debug_object_free - debug checks when an object is freed |
| 579 | * @addr: address of the object |
| 580 | * @descr: pointer to an object specific debug description structure |
| 581 | */ |
| 582 | void debug_object_free(void *addr, struct debug_obj_descr *descr) |
| 583 | { |
| 584 | enum debug_obj_state state; |
| 585 | struct debug_bucket *db; |
| 586 | struct debug_obj *obj; |
| 587 | unsigned long flags; |
| 588 | |
| 589 | if (!debug_objects_enabled) |
| 590 | return; |
| 591 | |
| 592 | db = get_bucket((unsigned long) addr); |
| 593 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 594 | raw_spin_lock_irqsave(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 595 | |
| 596 | obj = lookup_object(addr, db); |
| 597 | if (!obj) |
| 598 | goto out_unlock; |
| 599 | |
| 600 | switch (obj->state) { |
| 601 | case ODEBUG_STATE_ACTIVE: |
| 602 | debug_print_object(obj, "free"); |
| 603 | state = obj->state; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 604 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 605 | debug_object_fixup(descr->fixup_free, addr, state); |
| 606 | return; |
| 607 | default: |
| 608 | hlist_del(&obj->node); |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 609 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 610 | free_object(obj); |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 611 | return; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 612 | } |
| 613 | out_unlock: |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 614 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 615 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 616 | EXPORT_SYMBOL_GPL(debug_object_free); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 617 | |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 618 | /** |
Christine Chan | b84d435 | 2011-11-07 19:48:27 -0800 | [diff] [blame] | 619 | * debug_object_assert_init - debug checks when object should be init-ed |
| 620 | * @addr: address of the object |
| 621 | * @descr: pointer to an object specific debug description structure |
| 622 | */ |
| 623 | void debug_object_assert_init(void *addr, struct debug_obj_descr *descr) |
| 624 | { |
| 625 | struct debug_bucket *db; |
| 626 | struct debug_obj *obj; |
| 627 | unsigned long flags; |
| 628 | |
| 629 | if (!debug_objects_enabled) |
| 630 | return; |
| 631 | |
| 632 | db = get_bucket((unsigned long) addr); |
| 633 | |
| 634 | raw_spin_lock_irqsave(&db->lock, flags); |
| 635 | |
| 636 | obj = lookup_object(addr, db); |
| 637 | if (!obj) { |
| 638 | struct debug_obj o = { .object = addr, |
| 639 | .state = ODEBUG_STATE_NOTAVAILABLE, |
| 640 | .descr = descr }; |
| 641 | |
| 642 | raw_spin_unlock_irqrestore(&db->lock, flags); |
| 643 | /* |
Du, Changbin | b9fdac7 | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 644 | * Maybe the object is static, and we let the type specific |
| 645 | * code confirm. Track this static object if true, else invoke |
| 646 | * fixup. |
Christine Chan | b84d435 | 2011-11-07 19:48:27 -0800 | [diff] [blame] | 647 | */ |
Du, Changbin | b9fdac7 | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 648 | if (descr->is_static_object && descr->is_static_object(addr)) { |
| 649 | /* Track this static object */ |
| 650 | debug_object_init(addr, descr); |
| 651 | } else { |
Christine Chan | b84d435 | 2011-11-07 19:48:27 -0800 | [diff] [blame] | 652 | debug_print_object(&o, "assert_init"); |
Du, Changbin | b9fdac7 | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 653 | debug_object_fixup(descr->fixup_assert_init, addr, |
| 654 | ODEBUG_STATE_NOTAVAILABLE); |
| 655 | } |
Christine Chan | b84d435 | 2011-11-07 19:48:27 -0800 | [diff] [blame] | 656 | return; |
| 657 | } |
| 658 | |
| 659 | raw_spin_unlock_irqrestore(&db->lock, flags); |
| 660 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 661 | EXPORT_SYMBOL_GPL(debug_object_assert_init); |
Christine Chan | b84d435 | 2011-11-07 19:48:27 -0800 | [diff] [blame] | 662 | |
| 663 | /** |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 664 | * debug_object_active_state - debug checks object usage state machine |
| 665 | * @addr: address of the object |
| 666 | * @descr: pointer to an object specific debug description structure |
| 667 | * @expect: expected state |
| 668 | * @next: state to move to if expected state is found |
| 669 | */ |
| 670 | void |
| 671 | debug_object_active_state(void *addr, struct debug_obj_descr *descr, |
| 672 | unsigned int expect, unsigned int next) |
| 673 | { |
| 674 | struct debug_bucket *db; |
| 675 | struct debug_obj *obj; |
| 676 | unsigned long flags; |
| 677 | |
| 678 | if (!debug_objects_enabled) |
| 679 | return; |
| 680 | |
| 681 | db = get_bucket((unsigned long) addr); |
| 682 | |
| 683 | raw_spin_lock_irqsave(&db->lock, flags); |
| 684 | |
| 685 | obj = lookup_object(addr, db); |
| 686 | if (obj) { |
| 687 | switch (obj->state) { |
| 688 | case ODEBUG_STATE_ACTIVE: |
| 689 | if (obj->astate == expect) |
| 690 | obj->astate = next; |
| 691 | else |
| 692 | debug_print_object(obj, "active_state"); |
| 693 | break; |
| 694 | |
| 695 | default: |
| 696 | debug_print_object(obj, "active_state"); |
| 697 | break; |
| 698 | } |
| 699 | } else { |
| 700 | struct debug_obj o = { .object = addr, |
| 701 | .state = ODEBUG_STATE_NOTAVAILABLE, |
| 702 | .descr = descr }; |
| 703 | |
| 704 | debug_print_object(&o, "active_state"); |
| 705 | } |
| 706 | |
| 707 | raw_spin_unlock_irqrestore(&db->lock, flags); |
| 708 | } |
Chris Wilson | f8ff04e | 2016-11-30 15:54:10 -0800 | [diff] [blame] | 709 | EXPORT_SYMBOL_GPL(debug_object_active_state); |
Mathieu Desnoyers | a5d8e46 | 2010-04-17 08:48:38 -0400 | [diff] [blame] | 710 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 711 | #ifdef CONFIG_DEBUG_OBJECTS_FREE |
| 712 | static void __debug_check_no_obj_freed(const void *address, unsigned long size) |
| 713 | { |
| 714 | unsigned long flags, oaddr, saddr, eaddr, paddr, chunks; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 715 | struct hlist_node *tmp; |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 716 | HLIST_HEAD(freelist); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 717 | struct debug_obj_descr *descr; |
| 718 | enum debug_obj_state state; |
| 719 | struct debug_bucket *db; |
| 720 | struct debug_obj *obj; |
| 721 | int cnt; |
| 722 | |
| 723 | saddr = (unsigned long) address; |
| 724 | eaddr = saddr + size; |
| 725 | paddr = saddr & ODEBUG_CHUNK_MASK; |
| 726 | chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1)); |
| 727 | chunks >>= ODEBUG_CHUNK_SHIFT; |
| 728 | |
| 729 | for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) { |
| 730 | db = get_bucket(paddr); |
| 731 | |
| 732 | repeat: |
| 733 | cnt = 0; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 734 | raw_spin_lock_irqsave(&db->lock, flags); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 735 | hlist_for_each_entry_safe(obj, tmp, &db->list, node) { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 736 | cnt++; |
| 737 | oaddr = (unsigned long) obj->object; |
| 738 | if (oaddr < saddr || oaddr >= eaddr) |
| 739 | continue; |
| 740 | |
| 741 | switch (obj->state) { |
| 742 | case ODEBUG_STATE_ACTIVE: |
| 743 | debug_print_object(obj, "free"); |
| 744 | descr = obj->descr; |
| 745 | state = obj->state; |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 746 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 747 | debug_object_fixup(descr->fixup_free, |
| 748 | (void *) oaddr, state); |
| 749 | goto repeat; |
| 750 | default: |
| 751 | hlist_del(&obj->node); |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 752 | hlist_add_head(&obj->node, &freelist); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 753 | break; |
| 754 | } |
| 755 | } |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 756 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 757 | |
| 758 | /* Now free them */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 759 | hlist_for_each_entry_safe(obj, tmp, &freelist, node) { |
Vegard Nossum | 673d62cc | 2008-08-31 23:39:21 +0200 | [diff] [blame] | 760 | hlist_del(&obj->node); |
| 761 | free_object(obj); |
| 762 | } |
| 763 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 764 | if (cnt > debug_objects_maxchain) |
| 765 | debug_objects_maxchain = cnt; |
| 766 | } |
| 767 | } |
| 768 | |
| 769 | void debug_check_no_obj_freed(const void *address, unsigned long size) |
| 770 | { |
| 771 | if (debug_objects_enabled) |
| 772 | __debug_check_no_obj_freed(address, size); |
| 773 | } |
| 774 | #endif |
| 775 | |
| 776 | #ifdef CONFIG_DEBUG_FS |
| 777 | |
| 778 | static int debug_stats_show(struct seq_file *m, void *v) |
| 779 | { |
| 780 | seq_printf(m, "max_chain :%d\n", debug_objects_maxchain); |
| 781 | seq_printf(m, "warnings :%d\n", debug_objects_warnings); |
| 782 | seq_printf(m, "fixups :%d\n", debug_objects_fixups); |
| 783 | seq_printf(m, "pool_free :%d\n", obj_pool_free); |
| 784 | seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free); |
| 785 | seq_printf(m, "pool_used :%d\n", obj_pool_used); |
| 786 | seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used); |
Waiman Long | 0cad93c | 2017-02-07 16:40:30 -0500 | [diff] [blame] | 787 | seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated); |
| 788 | seq_printf(m, "objs_freed :%d\n", debug_objects_freed); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | static int debug_stats_open(struct inode *inode, struct file *filp) |
| 793 | { |
| 794 | return single_open(filp, debug_stats_show, NULL); |
| 795 | } |
| 796 | |
| 797 | static const struct file_operations debug_stats_fops = { |
| 798 | .open = debug_stats_open, |
| 799 | .read = seq_read, |
| 800 | .llseek = seq_lseek, |
| 801 | .release = single_release, |
| 802 | }; |
| 803 | |
| 804 | static int __init debug_objects_init_debugfs(void) |
| 805 | { |
| 806 | struct dentry *dbgdir, *dbgstats; |
| 807 | |
| 808 | if (!debug_objects_enabled) |
| 809 | return 0; |
| 810 | |
| 811 | dbgdir = debugfs_create_dir("debug_objects", NULL); |
| 812 | if (!dbgdir) |
| 813 | return -ENOMEM; |
| 814 | |
| 815 | dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL, |
| 816 | &debug_stats_fops); |
| 817 | if (!dbgstats) |
| 818 | goto err; |
| 819 | |
| 820 | return 0; |
| 821 | |
| 822 | err: |
| 823 | debugfs_remove(dbgdir); |
| 824 | |
| 825 | return -ENOMEM; |
| 826 | } |
| 827 | __initcall(debug_objects_init_debugfs); |
| 828 | |
| 829 | #else |
| 830 | static inline void debug_objects_init_debugfs(void) { } |
| 831 | #endif |
| 832 | |
| 833 | #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST |
| 834 | |
| 835 | /* Random data structure for the self test */ |
| 836 | struct self_test { |
| 837 | unsigned long dummy1[6]; |
| 838 | int static_init; |
| 839 | unsigned long dummy2[3]; |
| 840 | }; |
| 841 | |
| 842 | static __initdata struct debug_obj_descr descr_type_test; |
| 843 | |
Du, Changbin | b9fdac7 | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 844 | static bool __init is_static_object(void *addr) |
| 845 | { |
| 846 | struct self_test *obj = addr; |
| 847 | |
| 848 | return obj->static_init; |
| 849 | } |
| 850 | |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 851 | /* |
| 852 | * fixup_init is called when: |
| 853 | * - an active object is initialized |
| 854 | */ |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 855 | static bool __init fixup_init(void *addr, enum debug_obj_state state) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 856 | { |
| 857 | struct self_test *obj = addr; |
| 858 | |
| 859 | switch (state) { |
| 860 | case ODEBUG_STATE_ACTIVE: |
| 861 | debug_object_deactivate(obj, &descr_type_test); |
| 862 | debug_object_init(obj, &descr_type_test); |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 863 | return true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 864 | default: |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 865 | return false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 866 | } |
| 867 | } |
| 868 | |
| 869 | /* |
| 870 | * fixup_activate is called when: |
| 871 | * - an active object is activated |
Du, Changbin | b9fdac7 | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 872 | * - an unknown non-static object is activated |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 873 | */ |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 874 | static bool __init fixup_activate(void *addr, enum debug_obj_state state) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 875 | { |
| 876 | struct self_test *obj = addr; |
| 877 | |
| 878 | switch (state) { |
| 879 | case ODEBUG_STATE_NOTAVAILABLE: |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 880 | return true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 881 | case ODEBUG_STATE_ACTIVE: |
| 882 | debug_object_deactivate(obj, &descr_type_test); |
| 883 | debug_object_activate(obj, &descr_type_test); |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 884 | return true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 885 | |
| 886 | default: |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 887 | return false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 888 | } |
| 889 | } |
| 890 | |
| 891 | /* |
| 892 | * fixup_destroy is called when: |
| 893 | * - an active object is destroyed |
| 894 | */ |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 895 | static bool __init fixup_destroy(void *addr, enum debug_obj_state state) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 896 | { |
| 897 | struct self_test *obj = addr; |
| 898 | |
| 899 | switch (state) { |
| 900 | case ODEBUG_STATE_ACTIVE: |
| 901 | debug_object_deactivate(obj, &descr_type_test); |
| 902 | debug_object_destroy(obj, &descr_type_test); |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 903 | return true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 904 | default: |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 905 | return false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 906 | } |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * fixup_free is called when: |
| 911 | * - an active object is freed |
| 912 | */ |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 913 | static bool __init fixup_free(void *addr, enum debug_obj_state state) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 914 | { |
| 915 | struct self_test *obj = addr; |
| 916 | |
| 917 | switch (state) { |
| 918 | case ODEBUG_STATE_ACTIVE: |
| 919 | debug_object_deactivate(obj, &descr_type_test); |
| 920 | debug_object_free(obj, &descr_type_test); |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 921 | return true; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 922 | default: |
Du, Changbin | b1e4d9d | 2016-05-19 17:09:20 -0700 | [diff] [blame] | 923 | return false; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 924 | } |
| 925 | } |
| 926 | |
Henrik Kretzschmar | 1fb2f77 | 2010-03-26 20:38:35 +0100 | [diff] [blame] | 927 | static int __init |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 928 | check_results(void *addr, enum debug_obj_state state, int fixups, int warnings) |
| 929 | { |
| 930 | struct debug_bucket *db; |
| 931 | struct debug_obj *obj; |
| 932 | unsigned long flags; |
| 933 | int res = -EINVAL; |
| 934 | |
| 935 | db = get_bucket((unsigned long) addr); |
| 936 | |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 937 | raw_spin_lock_irqsave(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 938 | |
| 939 | obj = lookup_object(addr, db); |
| 940 | if (!obj && state != ODEBUG_STATE_NONE) { |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 941 | WARN(1, KERN_ERR "ODEBUG: selftest object not found\n"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 942 | goto out; |
| 943 | } |
| 944 | if (obj && obj->state != state) { |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 945 | WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n", |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 946 | obj->state, state); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 947 | goto out; |
| 948 | } |
| 949 | if (fixups != debug_objects_fixups) { |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 950 | WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n", |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 951 | fixups, debug_objects_fixups); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 952 | goto out; |
| 953 | } |
| 954 | if (warnings != debug_objects_warnings) { |
Arjan van de Ven | 5cd2b45 | 2008-07-25 19:45:39 -0700 | [diff] [blame] | 955 | WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n", |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 956 | warnings, debug_objects_warnings); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 957 | goto out; |
| 958 | } |
| 959 | res = 0; |
| 960 | out: |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 961 | raw_spin_unlock_irqrestore(&db->lock, flags); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 962 | if (res) |
| 963 | debug_objects_enabled = 0; |
| 964 | return res; |
| 965 | } |
| 966 | |
| 967 | static __initdata struct debug_obj_descr descr_type_test = { |
| 968 | .name = "selftest", |
Du, Changbin | b9fdac7 | 2016-05-19 17:09:41 -0700 | [diff] [blame] | 969 | .is_static_object = is_static_object, |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 970 | .fixup_init = fixup_init, |
| 971 | .fixup_activate = fixup_activate, |
| 972 | .fixup_destroy = fixup_destroy, |
| 973 | .fixup_free = fixup_free, |
| 974 | }; |
| 975 | |
| 976 | static __initdata struct self_test obj = { .static_init = 0 }; |
| 977 | |
| 978 | static void __init debug_objects_selftest(void) |
| 979 | { |
| 980 | int fixups, oldfixups, warnings, oldwarnings; |
| 981 | unsigned long flags; |
| 982 | |
| 983 | local_irq_save(flags); |
| 984 | |
| 985 | fixups = oldfixups = debug_objects_fixups; |
| 986 | warnings = oldwarnings = debug_objects_warnings; |
| 987 | descr_test = &descr_type_test; |
| 988 | |
| 989 | debug_object_init(&obj, &descr_type_test); |
| 990 | if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings)) |
| 991 | goto out; |
| 992 | debug_object_activate(&obj, &descr_type_test); |
| 993 | if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) |
| 994 | goto out; |
| 995 | debug_object_activate(&obj, &descr_type_test); |
| 996 | if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings)) |
| 997 | goto out; |
| 998 | debug_object_deactivate(&obj, &descr_type_test); |
| 999 | if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings)) |
| 1000 | goto out; |
| 1001 | debug_object_destroy(&obj, &descr_type_test); |
| 1002 | if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings)) |
| 1003 | goto out; |
| 1004 | debug_object_init(&obj, &descr_type_test); |
| 1005 | if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) |
| 1006 | goto out; |
| 1007 | debug_object_activate(&obj, &descr_type_test); |
| 1008 | if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) |
| 1009 | goto out; |
| 1010 | debug_object_deactivate(&obj, &descr_type_test); |
| 1011 | if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings)) |
| 1012 | goto out; |
| 1013 | debug_object_free(&obj, &descr_type_test); |
| 1014 | if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings)) |
| 1015 | goto out; |
| 1016 | |
| 1017 | obj.static_init = 1; |
| 1018 | debug_object_activate(&obj, &descr_type_test); |
Stephen Boyd | 9f78ff0 | 2012-03-05 14:59:17 -0800 | [diff] [blame] | 1019 | if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1020 | goto out; |
| 1021 | debug_object_init(&obj, &descr_type_test); |
| 1022 | if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings)) |
| 1023 | goto out; |
| 1024 | debug_object_free(&obj, &descr_type_test); |
| 1025 | if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings)) |
| 1026 | goto out; |
| 1027 | |
| 1028 | #ifdef CONFIG_DEBUG_OBJECTS_FREE |
| 1029 | debug_object_init(&obj, &descr_type_test); |
| 1030 | if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings)) |
| 1031 | goto out; |
| 1032 | debug_object_activate(&obj, &descr_type_test); |
| 1033 | if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) |
| 1034 | goto out; |
| 1035 | __debug_check_no_obj_freed(&obj, sizeof(obj)); |
| 1036 | if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings)) |
| 1037 | goto out; |
| 1038 | #endif |
Fabian Frederick | 719e484 | 2014-06-04 16:06:04 -0700 | [diff] [blame] | 1039 | pr_info("selftest passed\n"); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1040 | |
| 1041 | out: |
| 1042 | debug_objects_fixups = oldfixups; |
| 1043 | debug_objects_warnings = oldwarnings; |
| 1044 | descr_test = NULL; |
| 1045 | |
| 1046 | local_irq_restore(flags); |
| 1047 | } |
| 1048 | #else |
| 1049 | static inline void debug_objects_selftest(void) { } |
| 1050 | #endif |
| 1051 | |
| 1052 | /* |
| 1053 | * Called during early boot to initialize the hash buckets and link |
| 1054 | * the static object pool objects into the poll list. After this call |
| 1055 | * the object tracker is fully operational. |
| 1056 | */ |
| 1057 | void __init debug_objects_early_init(void) |
| 1058 | { |
| 1059 | int i; |
| 1060 | |
| 1061 | for (i = 0; i < ODEBUG_HASH_SIZE; i++) |
Thomas Gleixner | aef9cb0 | 2009-11-17 18:11:28 +0100 | [diff] [blame] | 1062 | raw_spin_lock_init(&obj_hash[i].lock); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1063 | |
| 1064 | for (i = 0; i < ODEBUG_POOL_SIZE; i++) |
| 1065 | hlist_add_head(&obj_static_pool[i].node, &obj_pool); |
| 1066 | } |
| 1067 | |
| 1068 | /* |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1069 | * Convert the statically allocated objects to dynamic ones: |
| 1070 | */ |
Henrik Kretzschmar | 1fb2f77 | 2010-03-26 20:38:35 +0100 | [diff] [blame] | 1071 | static int __init debug_objects_replace_static_objects(void) |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1072 | { |
| 1073 | struct debug_bucket *db = obj_hash; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1074 | struct hlist_node *tmp; |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1075 | struct debug_obj *obj, *new; |
| 1076 | HLIST_HEAD(objects); |
| 1077 | int i, cnt = 0; |
| 1078 | |
| 1079 | for (i = 0; i < ODEBUG_POOL_SIZE; i++) { |
| 1080 | obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL); |
| 1081 | if (!obj) |
| 1082 | goto free; |
| 1083 | hlist_add_head(&obj->node, &objects); |
| 1084 | } |
| 1085 | |
| 1086 | /* |
| 1087 | * When debug_objects_mem_init() is called we know that only |
| 1088 | * one CPU is up, so disabling interrupts is enough |
| 1089 | * protection. This avoids the lockdep hell of lock ordering. |
| 1090 | */ |
| 1091 | local_irq_disable(); |
| 1092 | |
| 1093 | /* Remove the statically allocated objects from the pool */ |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1094 | hlist_for_each_entry_safe(obj, tmp, &obj_pool, node) |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1095 | hlist_del(&obj->node); |
| 1096 | /* Move the allocated objects to the pool */ |
| 1097 | hlist_move_list(&objects, &obj_pool); |
| 1098 | |
| 1099 | /* Replace the active object references */ |
| 1100 | for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) { |
| 1101 | hlist_move_list(&db->list, &objects); |
| 1102 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1103 | hlist_for_each_entry(obj, &objects, node) { |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1104 | new = hlist_entry(obj_pool.first, typeof(*obj), node); |
| 1105 | hlist_del(&new->node); |
| 1106 | /* copy object data */ |
| 1107 | *new = *obj; |
| 1108 | hlist_add_head(&new->node, &db->list); |
| 1109 | cnt++; |
| 1110 | } |
| 1111 | } |
Thomas Gleixner | 765a5e0 | 2012-04-11 11:54:27 +0200 | [diff] [blame] | 1112 | local_irq_enable(); |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1113 | |
Fabian Frederick | c0f35cc | 2014-06-04 16:06:05 -0700 | [diff] [blame] | 1114 | pr_debug("%d of %d active objects replaced\n", |
| 1115 | cnt, obj_pool_used); |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1116 | return 0; |
| 1117 | free: |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1118 | hlist_for_each_entry_safe(obj, tmp, &objects, node) { |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1119 | hlist_del(&obj->node); |
| 1120 | kmem_cache_free(obj_cache, obj); |
| 1121 | } |
| 1122 | return -ENOMEM; |
| 1123 | } |
| 1124 | |
| 1125 | /* |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1126 | * Called after the kmem_caches are functional to setup a dedicated |
| 1127 | * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag |
| 1128 | * prevents that the debug code is called on kmem_cache_free() for the |
| 1129 | * debug tracker objects to avoid recursive calls. |
| 1130 | */ |
| 1131 | void __init debug_objects_mem_init(void) |
| 1132 | { |
| 1133 | if (!debug_objects_enabled) |
| 1134 | return; |
| 1135 | |
| 1136 | obj_cache = kmem_cache_create("debug_objects_cache", |
| 1137 | sizeof (struct debug_obj), 0, |
| 1138 | SLAB_DEBUG_OBJECTS, NULL); |
| 1139 | |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1140 | if (!obj_cache || debug_objects_replace_static_objects()) { |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1141 | debug_objects_enabled = 0; |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1142 | if (obj_cache) |
| 1143 | kmem_cache_destroy(obj_cache); |
Fabian Frederick | 719e484 | 2014-06-04 16:06:04 -0700 | [diff] [blame] | 1144 | pr_warn("out of memory.\n"); |
Thomas Gleixner | 1be1cb7 | 2009-03-16 18:53:18 +0100 | [diff] [blame] | 1145 | } else |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1146 | debug_objects_selftest(); |
Waiman Long | 97dd552 | 2017-01-05 15:17:04 -0500 | [diff] [blame] | 1147 | |
| 1148 | /* |
| 1149 | * Increase the thresholds for allocating and freeing objects |
| 1150 | * according to the number of possible CPUs available in the system. |
| 1151 | */ |
| 1152 | debug_objects_pool_size += num_possible_cpus() * 32; |
| 1153 | debug_objects_pool_min_level += num_possible_cpus() * 4; |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 1154 | } |