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