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