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