Commit | Line | Data |
---|---|---|
3ac7fe5a TG |
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 | */ | |
719e4843 FF |
10 | |
11 | #define pr_fmt(fmt) "ODEBUG: " fmt | |
12 | ||
3ac7fe5a TG |
13 | #include <linux/debugobjects.h> |
14 | #include <linux/interrupt.h> | |
d43c36dc | 15 | #include <linux/sched.h> |
68db0cf1 | 16 | #include <linux/sched/task_stack.h> |
3ac7fe5a TG |
17 | #include <linux/seq_file.h> |
18 | #include <linux/debugfs.h> | |
5a0e3ad6 | 19 | #include <linux/slab.h> |
3ac7fe5a | 20 | #include <linux/hash.h> |
caba4cbb | 21 | #include <linux/kmemleak.h> |
3ac7fe5a TG |
22 | |
23 | #define ODEBUG_HASH_BITS 14 | |
24 | #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS) | |
25 | ||
0b6ec8c0 | 26 | #define ODEBUG_POOL_SIZE 1024 |
3ac7fe5a TG |
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; | |
aef9cb05 | 35 | raw_spinlock_t lock; |
3ac7fe5a TG |
36 | }; |
37 | ||
38 | static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE]; | |
39 | ||
1be1cb7b | 40 | static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata; |
3ac7fe5a | 41 | |
aef9cb05 | 42 | static DEFINE_RAW_SPINLOCK(pool_lock); |
3ac7fe5a TG |
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; | |
bd9dcd04 | 53 | static int debug_objects_maxchecked __read_mostly; |
3ac7fe5a TG |
54 | static int debug_objects_fixups __read_mostly; |
55 | static int debug_objects_warnings __read_mostly; | |
3ae70205 IM |
56 | static int debug_objects_enabled __read_mostly |
57 | = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT; | |
97dd552e WL |
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; | |
3ac7fe5a TG |
62 | static struct debug_obj_descr *descr_test __read_mostly; |
63 | ||
c4b73aab | 64 | /* |
0cad93c3 | 65 | * Track numbers of kmem_cache_alloc()/free() calls done. |
c4b73aab | 66 | */ |
0cad93c3 | 67 | static int debug_objects_allocated; |
c4b73aab WL |
68 | static int debug_objects_freed; |
69 | ||
337fff8b TG |
70 | static void free_obj_work(struct work_struct *work); |
71 | static DECLARE_WORK(debug_obj_work, free_obj_work); | |
72 | ||
3ac7fe5a TG |
73 | static int __init enable_object_debug(char *str) |
74 | { | |
75 | debug_objects_enabled = 1; | |
76 | return 0; | |
77 | } | |
3e8ebb5c KM |
78 | |
79 | static int __init disable_object_debug(char *str) | |
80 | { | |
81 | debug_objects_enabled = 0; | |
82 | return 0; | |
83 | } | |
84 | ||
3ac7fe5a | 85 | early_param("debug_objects", enable_object_debug); |
3e8ebb5c | 86 | early_param("no_debug_objects", disable_object_debug); |
3ac7fe5a TG |
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 | ||
1fda107d | 97 | static void fill_pool(void) |
3ac7fe5a TG |
98 | { |
99 | gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN; | |
100 | struct debug_obj *new; | |
50db04dd | 101 | unsigned long flags; |
3ac7fe5a | 102 | |
97dd552e | 103 | if (likely(obj_pool_free >= debug_objects_pool_min_level)) |
1fda107d | 104 | return; |
3ac7fe5a TG |
105 | |
106 | if (unlikely(!obj_cache)) | |
1fda107d | 107 | return; |
3ac7fe5a | 108 | |
97dd552e | 109 | while (obj_pool_free < debug_objects_pool_min_level) { |
3ac7fe5a TG |
110 | |
111 | new = kmem_cache_zalloc(obj_cache, gfp); | |
112 | if (!new) | |
3340808c | 113 | return; |
3ac7fe5a | 114 | |
caba4cbb | 115 | kmemleak_ignore(new); |
aef9cb05 | 116 | raw_spin_lock_irqsave(&pool_lock, flags); |
3ac7fe5a | 117 | hlist_add_head(&new->node, &obj_pool); |
0cad93c3 | 118 | debug_objects_allocated++; |
3ac7fe5a | 119 | obj_pool_free++; |
aef9cb05 | 120 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
3ac7fe5a | 121 | } |
3ac7fe5a TG |
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 | { | |
3ac7fe5a TG |
129 | struct debug_obj *obj; |
130 | int cnt = 0; | |
131 | ||
b67bfe0d | 132 | hlist_for_each_entry(obj, &b->list, node) { |
3ac7fe5a TG |
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 | /* | |
50db04dd | 144 | * Allocate a new object. If the pool is empty, switch off the debugger. |
673d62cc | 145 | * Must be called with interrupts disabled. |
3ac7fe5a TG |
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; | |
3ac7fe5a | 151 | |
aef9cb05 | 152 | raw_spin_lock(&pool_lock); |
3ac7fe5a TG |
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; | |
a5d8e467 | 159 | obj->astate = 0; |
3ac7fe5a TG |
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 | } | |
aef9cb05 | 172 | raw_spin_unlock(&pool_lock); |
3ac7fe5a | 173 | |
3ac7fe5a TG |
174 | return obj; |
175 | } | |
176 | ||
177 | /* | |
337fff8b | 178 | * workqueue function to free objects. |
858274b6 WL |
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. | |
3ac7fe5a | 183 | */ |
858274b6 WL |
184 | #define ODEBUG_FREE_BATCH 4 |
185 | ||
337fff8b | 186 | static void free_obj_work(struct work_struct *work) |
3ac7fe5a | 187 | { |
858274b6 | 188 | struct debug_obj *objs[ODEBUG_FREE_BATCH]; |
673d62cc | 189 | unsigned long flags; |
858274b6 | 190 | int i; |
3ac7fe5a | 191 | |
858274b6 WL |
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; | |
337fff8b TG |
203 | /* |
204 | * We release pool_lock across kmem_cache_free() to | |
205 | * avoid contention on pool_lock. | |
206 | */ | |
aef9cb05 | 207 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
858274b6 WL |
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; | |
3ac7fe5a | 212 | } |
aef9cb05 | 213 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
337fff8b TG |
214 | } |
215 | ||
216 | /* | |
217 | * Put the object back into the pool and schedule work to free objects | |
218 | * if necessary. | |
219 | */ | |
220 | static void free_object(struct debug_obj *obj) | |
221 | { | |
222 | unsigned long flags; | |
223 | int sched = 0; | |
224 | ||
aef9cb05 | 225 | raw_spin_lock_irqsave(&pool_lock, flags); |
337fff8b TG |
226 | /* |
227 | * schedule work when the pool is filled and the cache is | |
228 | * initialized: | |
229 | */ | |
97dd552e | 230 | if (obj_pool_free > debug_objects_pool_size && obj_cache) |
7092dff2 | 231 | sched = 1; |
337fff8b TG |
232 | hlist_add_head(&obj->node, &obj_pool); |
233 | obj_pool_free++; | |
234 | obj_pool_used--; | |
aef9cb05 | 235 | raw_spin_unlock_irqrestore(&pool_lock, flags); |
337fff8b TG |
236 | if (sched) |
237 | schedule_work(&debug_obj_work); | |
3ac7fe5a TG |
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; | |
b67bfe0d | 247 | struct hlist_node *tmp; |
673d62cc | 248 | HLIST_HEAD(freelist); |
3ac7fe5a TG |
249 | struct debug_obj *obj; |
250 | unsigned long flags; | |
251 | int i; | |
252 | ||
719e4843 | 253 | pr_warn("Out of memory. ODEBUG disabled\n"); |
3ac7fe5a TG |
254 | |
255 | for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) { | |
aef9cb05 | 256 | raw_spin_lock_irqsave(&db->lock, flags); |
673d62cc | 257 | hlist_move_list(&db->list, &freelist); |
aef9cb05 | 258 | raw_spin_unlock_irqrestore(&db->lock, flags); |
673d62cc VN |
259 | |
260 | /* Now free them */ | |
b67bfe0d | 261 | hlist_for_each_entry_safe(obj, tmp, &freelist, node) { |
3ac7fe5a TG |
262 | hlist_del(&obj->node); |
263 | free_object(obj); | |
264 | } | |
3ac7fe5a TG |
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 | { | |
99777288 | 282 | struct debug_obj_descr *descr = obj->descr; |
3ac7fe5a TG |
283 | static int limit; |
284 | ||
99777288 SG |
285 | if (limit < 5 && descr != descr_test) { |
286 | void *hint = descr->debug_hint ? | |
287 | descr->debug_hint(obj->object) : NULL; | |
3ac7fe5a | 288 | limit++; |
a5d8e467 | 289 | WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) " |
99777288 | 290 | "object type: %s hint: %pS\n", |
a5d8e467 | 291 | msg, obj_states[obj->state], obj->astate, |
99777288 | 292 | descr->name, hint); |
3ac7fe5a TG |
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 | */ | |
b1e4d9d8 DC |
301 | static bool |
302 | debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state), | |
3ac7fe5a TG |
303 | void * addr, enum debug_obj_state state) |
304 | { | |
b1e4d9d8 DC |
305 | if (fixup && fixup(addr, state)) { |
306 | debug_objects_fixups++; | |
307 | return true; | |
308 | } | |
309 | return false; | |
3ac7fe5a TG |
310 | } |
311 | ||
312 | static void debug_object_is_on_stack(void *addr, int onstack) | |
313 | { | |
3ac7fe5a TG |
314 | int is_on_stack; |
315 | static int limit; | |
316 | ||
317 | if (limit > 4) | |
318 | return; | |
319 | ||
8b05c7e6 | 320 | is_on_stack = object_is_on_stack(addr); |
3ac7fe5a TG |
321 | if (is_on_stack == onstack) |
322 | return; | |
323 | ||
324 | limit++; | |
325 | if (is_on_stack) | |
719e4843 | 326 | pr_warn("object is on stack, but not annotated\n"); |
3ac7fe5a | 327 | else |
719e4843 | 328 | pr_warn("object is not on stack, but annotated\n"); |
3ac7fe5a TG |
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 | ||
50db04dd VN |
340 | fill_pool(); |
341 | ||
3ac7fe5a TG |
342 | db = get_bucket((unsigned long) addr); |
343 | ||
aef9cb05 | 344 | raw_spin_lock_irqsave(&db->lock, flags); |
3ac7fe5a TG |
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; | |
aef9cb05 | 351 | raw_spin_unlock_irqrestore(&db->lock, flags); |
3ac7fe5a TG |
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; | |
aef9cb05 | 368 | raw_spin_unlock_irqrestore(&db->lock, flags); |
3ac7fe5a TG |
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 | ||
aef9cb05 | 379 | raw_spin_unlock_irqrestore(&db->lock, flags); |
3ac7fe5a TG |
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 | } | |
f8ff04e2 | 394 | EXPORT_SYMBOL_GPL(debug_object_init); |
3ac7fe5a TG |
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 | } | |
f8ff04e2 | 409 | EXPORT_SYMBOL_GPL(debug_object_init_on_stack); |
3ac7fe5a TG |
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 | |
b778ae25 | 415 | * Returns 0 for success, -EINVAL for check failed. |
3ac7fe5a | 416 | */ |
b778ae25 | 417 | int debug_object_activate(void *addr, struct debug_obj_descr *descr) |
3ac7fe5a TG |
418 | { |
419 | enum debug_obj_state state; | |
420 | struct debug_bucket *db; | |
421 | struct debug_obj *obj; | |
422 | unsigned long flags; | |
b778ae25 | 423 | int ret; |
feac18dd SB |
424 | struct debug_obj o = { .object = addr, |
425 | .state = ODEBUG_STATE_NOTAVAILABLE, | |
426 | .descr = descr }; | |
3ac7fe5a TG |
427 | |
428 | if (!debug_objects_enabled) | |
b778ae25 | 429 | return 0; |
3ac7fe5a TG |
430 | |
431 | db = get_bucket((unsigned long) addr); | |
432 | ||
aef9cb05 | 433 | raw_spin_lock_irqsave(&db->lock, flags); |
3ac7fe5a TG |
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; | |
b778ae25 | 441 | ret = 0; |
3ac7fe5a TG |
442 | break; |
443 | ||
444 | case ODEBUG_STATE_ACTIVE: | |
445 | debug_print_object(obj, "activate"); | |
446 | state = obj->state; | |
aef9cb05 | 447 | raw_spin_unlock_irqrestore(&db->lock, flags); |
b778ae25 | 448 | ret = debug_object_fixup(descr->fixup_activate, addr, state); |
e7a8e78b | 449 | return ret ? 0 : -EINVAL; |
3ac7fe5a TG |
450 | |
451 | case ODEBUG_STATE_DESTROYED: | |
452 | debug_print_object(obj, "activate"); | |
b778ae25 | 453 | ret = -EINVAL; |
3ac7fe5a TG |
454 | break; |
455 | default: | |
b778ae25 | 456 | ret = 0; |
3ac7fe5a TG |
457 | break; |
458 | } | |
aef9cb05 | 459 | raw_spin_unlock_irqrestore(&db->lock, flags); |
b778ae25 | 460 | return ret; |
3ac7fe5a TG |
461 | } |
462 | ||
aef9cb05 | 463 | raw_spin_unlock_irqrestore(&db->lock, flags); |
3ac7fe5a | 464 | /* |
b9fdac7f DC |
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. | |
3ac7fe5a | 470 | */ |
b9fdac7f DC |
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 { | |
feac18dd | 476 | debug_print_object(&o, "activate"); |
b9fdac7f DC |
477 | ret = debug_object_fixup(descr->fixup_activate, addr, |
478 | ODEBUG_STATE_NOTAVAILABLE); | |
479 | return ret ? 0 : -EINVAL; | |
b778ae25 PM |
480 | } |
481 | return 0; | |
3ac7fe5a | 482 | } |
f8ff04e2 | 483 | EXPORT_SYMBOL_GPL(debug_object_activate); |
3ac7fe5a TG |
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 | ||
aef9cb05 | 501 | raw_spin_lock_irqsave(&db->lock, flags); |
3ac7fe5a TG |
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: | |
a5d8e467 MD |
509 | if (!obj->astate) |
510 | obj->state = ODEBUG_STATE_INACTIVE; | |
511 | else | |
512 | debug_print_object(obj, "deactivate"); | |
3ac7fe5a TG |
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 | ||
aef9cb05 | 529 | raw_spin_unlock_irqrestore(&db->lock, flags); |
3ac7fe5a | 530 | } |
f8ff04e2 | 531 | EXPORT_SYMBOL_GPL(debug_object_deactivate); |
3ac7fe5a TG |
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 | ||
aef9cb05 | 550 | raw_spin_lock_irqsave(&db->lock, flags); |
3ac7fe5a TG |
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; | |
aef9cb05 | 565 | raw_spin_unlock_irqrestore(&db->lock, flags); |
3ac7fe5a TG |
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: | |
aef9cb05 | 576 | raw_spin_unlock_irqrestore(&db->lock, flags); |
3ac7fe5a | 577 | } |
f8ff04e2 | 578 | EXPORT_SYMBOL_GPL(debug_object_destroy); |
3ac7fe5a TG |
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 | ||
aef9cb05 | 597 | raw_spin_lock_irqsave(&db->lock, flags); |
3ac7fe5a TG |
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; | |
aef9cb05 | 607 | raw_spin_unlock_irqrestore(&db->lock, flags); |
3ac7fe5a TG |
608 | debug_object_fixup(descr->fixup_free, addr, state); |
609 | return; | |
610 | default: | |
611 | hlist_del(&obj->node); | |
aef9cb05 | 612 | raw_spin_unlock_irqrestore(&db->lock, flags); |
3ac7fe5a | 613 | free_object(obj); |
673d62cc | 614 | return; |
3ac7fe5a TG |
615 | } |
616 | out_unlock: | |
aef9cb05 | 617 | raw_spin_unlock_irqrestore(&db->lock, flags); |
3ac7fe5a | 618 | } |
f8ff04e2 | 619 | EXPORT_SYMBOL_GPL(debug_object_free); |
3ac7fe5a | 620 | |
b84d435c CC |
621 | /** |
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 | /* | |
b9fdac7f DC |
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. | |
b84d435c | 650 | */ |
b9fdac7f DC |
651 | if (descr->is_static_object && descr->is_static_object(addr)) { |
652 | /* Track this static object */ | |
653 | debug_object_init(addr, descr); | |
654 | } else { | |
b84d435c | 655 | debug_print_object(&o, "assert_init"); |
b9fdac7f DC |
656 | debug_object_fixup(descr->fixup_assert_init, addr, |
657 | ODEBUG_STATE_NOTAVAILABLE); | |
658 | } | |
b84d435c CC |
659 | return; |
660 | } | |
661 | ||
662 | raw_spin_unlock_irqrestore(&db->lock, flags); | |
663 | } | |
f8ff04e2 | 664 | EXPORT_SYMBOL_GPL(debug_object_assert_init); |
b84d435c | 665 | |
a5d8e467 MD |
666 | /** |
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 | } | |
f8ff04e2 | 712 | EXPORT_SYMBOL_GPL(debug_object_active_state); |
a5d8e467 | 713 | |
3ac7fe5a TG |
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; | |
b67bfe0d | 718 | struct hlist_node *tmp; |
673d62cc | 719 | HLIST_HEAD(freelist); |
3ac7fe5a TG |
720 | struct debug_obj_descr *descr; |
721 | enum debug_obj_state state; | |
722 | struct debug_bucket *db; | |
723 | struct debug_obj *obj; | |
bd9dcd04 | 724 | int cnt, objs_checked = 0; |
3ac7fe5a TG |
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; | |
aef9cb05 | 737 | raw_spin_lock_irqsave(&db->lock, flags); |
b67bfe0d | 738 | hlist_for_each_entry_safe(obj, tmp, &db->list, node) { |
3ac7fe5a TG |
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; | |
aef9cb05 | 749 | raw_spin_unlock_irqrestore(&db->lock, flags); |
3ac7fe5a TG |
750 | debug_object_fixup(descr->fixup_free, |
751 | (void *) oaddr, state); | |
752 | goto repeat; | |
753 | default: | |
754 | hlist_del(&obj->node); | |
673d62cc | 755 | hlist_add_head(&obj->node, &freelist); |
3ac7fe5a TG |
756 | break; |
757 | } | |
758 | } | |
aef9cb05 | 759 | raw_spin_unlock_irqrestore(&db->lock, flags); |
673d62cc VN |
760 | |
761 | /* Now free them */ | |
b67bfe0d | 762 | hlist_for_each_entry_safe(obj, tmp, &freelist, node) { |
673d62cc VN |
763 | hlist_del(&obj->node); |
764 | free_object(obj); | |
765 | } | |
766 | ||
3ac7fe5a TG |
767 | if (cnt > debug_objects_maxchain) |
768 | debug_objects_maxchain = cnt; | |
bd9dcd04 YS |
769 | |
770 | objs_checked += cnt; | |
3ac7fe5a | 771 | } |
bd9dcd04 YS |
772 | |
773 | if (objs_checked > debug_objects_maxchecked) | |
774 | debug_objects_maxchecked = objs_checked; | |
3ac7fe5a TG |
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); | |
bd9dcd04 | 789 | seq_printf(m, "max_checked :%d\n", debug_objects_maxchecked); |
3ac7fe5a TG |
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); | |
0cad93c3 WL |
796 | seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated); |
797 | seq_printf(m, "objs_freed :%d\n", debug_objects_freed); | |
3ac7fe5a TG |
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 | ||
b9fdac7f DC |
853 | static bool __init is_static_object(void *addr) |
854 | { | |
855 | struct self_test *obj = addr; | |
856 | ||
857 | return obj->static_init; | |
858 | } | |
859 | ||
3ac7fe5a TG |
860 | /* |
861 | * fixup_init is called when: | |
862 | * - an active object is initialized | |
863 | */ | |
b1e4d9d8 | 864 | static bool __init fixup_init(void *addr, enum debug_obj_state state) |
3ac7fe5a TG |
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); | |
b1e4d9d8 | 872 | return true; |
3ac7fe5a | 873 | default: |
b1e4d9d8 | 874 | return false; |
3ac7fe5a TG |
875 | } |
876 | } | |
877 | ||
878 | /* | |
879 | * fixup_activate is called when: | |
880 | * - an active object is activated | |
b9fdac7f | 881 | * - an unknown non-static object is activated |
3ac7fe5a | 882 | */ |
b1e4d9d8 | 883 | static bool __init fixup_activate(void *addr, enum debug_obj_state state) |
3ac7fe5a TG |
884 | { |
885 | struct self_test *obj = addr; | |
886 | ||
887 | switch (state) { | |
888 | case ODEBUG_STATE_NOTAVAILABLE: | |
b1e4d9d8 | 889 | return true; |
3ac7fe5a TG |
890 | case ODEBUG_STATE_ACTIVE: |
891 | debug_object_deactivate(obj, &descr_type_test); | |
892 | debug_object_activate(obj, &descr_type_test); | |
b1e4d9d8 | 893 | return true; |
3ac7fe5a TG |
894 | |
895 | default: | |
b1e4d9d8 | 896 | return false; |
3ac7fe5a TG |
897 | } |
898 | } | |
899 | ||
900 | /* | |
901 | * fixup_destroy is called when: | |
902 | * - an active object is destroyed | |
903 | */ | |
b1e4d9d8 | 904 | static bool __init fixup_destroy(void *addr, enum debug_obj_state state) |
3ac7fe5a TG |
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); | |
b1e4d9d8 | 912 | return true; |
3ac7fe5a | 913 | default: |
b1e4d9d8 | 914 | return false; |
3ac7fe5a TG |
915 | } |
916 | } | |
917 | ||
918 | /* | |
919 | * fixup_free is called when: | |
920 | * - an active object is freed | |
921 | */ | |
b1e4d9d8 | 922 | static bool __init fixup_free(void *addr, enum debug_obj_state state) |
3ac7fe5a TG |
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); | |
b1e4d9d8 | 930 | return true; |
3ac7fe5a | 931 | default: |
b1e4d9d8 | 932 | return false; |
3ac7fe5a TG |
933 | } |
934 | } | |
935 | ||
1fb2f77c | 936 | static int __init |
3ac7fe5a TG |
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 | ||
aef9cb05 | 946 | raw_spin_lock_irqsave(&db->lock, flags); |
3ac7fe5a TG |
947 | |
948 | obj = lookup_object(addr, db); | |
949 | if (!obj && state != ODEBUG_STATE_NONE) { | |
5cd2b459 | 950 | WARN(1, KERN_ERR "ODEBUG: selftest object not found\n"); |
3ac7fe5a TG |
951 | goto out; |
952 | } | |
953 | if (obj && obj->state != state) { | |
5cd2b459 | 954 | WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n", |
3ac7fe5a | 955 | obj->state, state); |
3ac7fe5a TG |
956 | goto out; |
957 | } | |
958 | if (fixups != debug_objects_fixups) { | |
5cd2b459 | 959 | WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n", |
3ac7fe5a | 960 | fixups, debug_objects_fixups); |
3ac7fe5a TG |
961 | goto out; |
962 | } | |
963 | if (warnings != debug_objects_warnings) { | |
5cd2b459 | 964 | WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n", |
3ac7fe5a | 965 | warnings, debug_objects_warnings); |
3ac7fe5a TG |
966 | goto out; |
967 | } | |
968 | res = 0; | |
969 | out: | |
aef9cb05 | 970 | raw_spin_unlock_irqrestore(&db->lock, flags); |
3ac7fe5a TG |
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", | |
b9fdac7f | 978 | .is_static_object = is_static_object, |
3ac7fe5a TG |
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); | |
9f78ff00 | 1028 | if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings)) |
3ac7fe5a TG |
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 | |
719e4843 | 1048 | pr_info("selftest passed\n"); |
3ac7fe5a TG |
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++) | |
aef9cb05 | 1071 | raw_spin_lock_init(&obj_hash[i].lock); |
3ac7fe5a TG |
1072 | |
1073 | for (i = 0; i < ODEBUG_POOL_SIZE; i++) | |
1074 | hlist_add_head(&obj_static_pool[i].node, &obj_pool); | |
1075 | } | |
1076 | ||
1be1cb7b TG |
1077 | /* |
1078 | * Convert the statically allocated objects to dynamic ones: | |
1079 | */ | |
1fb2f77c | 1080 | static int __init debug_objects_replace_static_objects(void) |
1be1cb7b TG |
1081 | { |
1082 | struct debug_bucket *db = obj_hash; | |
b67bfe0d | 1083 | struct hlist_node *tmp; |
1be1cb7b TG |
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; | |
caba4cbb | 1092 | kmemleak_ignore(obj); |
1be1cb7b TG |
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 */ | |
b67bfe0d | 1104 | hlist_for_each_entry_safe(obj, tmp, &obj_pool, node) |
1be1cb7b TG |
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 | ||
b67bfe0d | 1113 | hlist_for_each_entry(obj, &objects, node) { |
1be1cb7b TG |
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 | } | |
765a5e0c | 1122 | local_irq_enable(); |
1be1cb7b | 1123 | |
c0f35cc0 FF |
1124 | pr_debug("%d of %d active objects replaced\n", |
1125 | cnt, obj_pool_used); | |
1be1cb7b TG |
1126 | return 0; |
1127 | free: | |
b67bfe0d | 1128 | hlist_for_each_entry_safe(obj, tmp, &objects, node) { |
1be1cb7b TG |
1129 | hlist_del(&obj->node); |
1130 | kmem_cache_free(obj_cache, obj); | |
1131 | } | |
1132 | return -ENOMEM; | |
1133 | } | |
1134 | ||
3ac7fe5a TG |
1135 | /* |
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 | ||
1be1cb7b | 1150 | if (!obj_cache || debug_objects_replace_static_objects()) { |
3ac7fe5a | 1151 | debug_objects_enabled = 0; |
1be1cb7b TG |
1152 | if (obj_cache) |
1153 | kmem_cache_destroy(obj_cache); | |
719e4843 | 1154 | pr_warn("out of memory.\n"); |
1be1cb7b | 1155 | } else |
3ac7fe5a | 1156 | debug_objects_selftest(); |
97dd552e WL |
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; | |
3ac7fe5a | 1164 | } |