blob: b07b5b82f39654921cd365837557aa721b3fd57e [file] [log] [blame]
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001/*
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 */
10#include <linux/debugobjects.h>
11#include <linux/interrupt.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040012#include <linux/sched.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070013#include <linux/seq_file.h>
14#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070016#include <linux/hash.h>
17
18#define ODEBUG_HASH_BITS 14
19#define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
20
21#define ODEBUG_POOL_SIZE 512
22#define ODEBUG_POOL_MIN_LEVEL 256
23
24#define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
25#define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
26#define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
27
28struct debug_bucket {
29 struct hlist_head list;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010030 raw_spinlock_t lock;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070031};
32
33static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
34
Thomas Gleixner1be1cb72009-03-16 18:53:18 +010035static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070036
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010037static DEFINE_RAW_SPINLOCK(pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070038
39static HLIST_HEAD(obj_pool);
40
41static int obj_pool_min_free = ODEBUG_POOL_SIZE;
42static int obj_pool_free = ODEBUG_POOL_SIZE;
43static int obj_pool_used;
44static int obj_pool_max_used;
45static struct kmem_cache *obj_cache;
46
47static int debug_objects_maxchain __read_mostly;
48static int debug_objects_fixups __read_mostly;
49static int debug_objects_warnings __read_mostly;
Ingo Molnar3ae70202008-11-26 10:02:00 +010050static int debug_objects_enabled __read_mostly
51 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
52
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070053static struct debug_obj_descr *descr_test __read_mostly;
54
Thomas Gleixner337fff82009-03-16 10:04:53 +010055static void free_obj_work(struct work_struct *work);
56static DECLARE_WORK(debug_obj_work, free_obj_work);
57
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070058static int __init enable_object_debug(char *str)
59{
60 debug_objects_enabled = 1;
61 return 0;
62}
Kyle McMartin3e8ebb52009-03-01 20:41:41 -050063
64static int __init disable_object_debug(char *str)
65{
66 debug_objects_enabled = 0;
67 return 0;
68}
69
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070070early_param("debug_objects", enable_object_debug);
Kyle McMartin3e8ebb52009-03-01 20:41:41 -050071early_param("no_debug_objects", disable_object_debug);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070072
73static const char *obj_states[ODEBUG_STATE_MAX] = {
74 [ODEBUG_STATE_NONE] = "none",
75 [ODEBUG_STATE_INIT] = "initialized",
76 [ODEBUG_STATE_INACTIVE] = "inactive",
77 [ODEBUG_STATE_ACTIVE] = "active",
78 [ODEBUG_STATE_DESTROYED] = "destroyed",
79 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
80};
81
82static int fill_pool(void)
83{
84 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
85 struct debug_obj *new;
Vegard Nossum50db04d2008-06-15 00:47:36 +020086 unsigned long flags;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070087
88 if (likely(obj_pool_free >= ODEBUG_POOL_MIN_LEVEL))
89 return obj_pool_free;
90
91 if (unlikely(!obj_cache))
92 return obj_pool_free;
93
94 while (obj_pool_free < ODEBUG_POOL_MIN_LEVEL) {
95
96 new = kmem_cache_zalloc(obj_cache, gfp);
97 if (!new)
98 return obj_pool_free;
99
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100100 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700101 hlist_add_head(&new->node, &obj_pool);
102 obj_pool_free++;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100103 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700104 }
105 return obj_pool_free;
106}
107
108/*
109 * Lookup an object in the hash bucket.
110 */
111static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
112{
113 struct hlist_node *node;
114 struct debug_obj *obj;
115 int cnt = 0;
116
117 hlist_for_each_entry(obj, node, &b->list, node) {
118 cnt++;
119 if (obj->object == addr)
120 return obj;
121 }
122 if (cnt > debug_objects_maxchain)
123 debug_objects_maxchain = cnt;
124
125 return NULL;
126}
127
128/*
Vegard Nossum50db04d2008-06-15 00:47:36 +0200129 * Allocate a new object. If the pool is empty, switch off the debugger.
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200130 * Must be called with interrupts disabled.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700131 */
132static struct debug_obj *
133alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
134{
135 struct debug_obj *obj = NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700136
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100137 raw_spin_lock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700138 if (obj_pool.first) {
139 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
140
141 obj->object = addr;
142 obj->descr = descr;
143 obj->state = ODEBUG_STATE_NONE;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400144 obj->astate = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700145 hlist_del(&obj->node);
146
147 hlist_add_head(&obj->node, &b->list);
148
149 obj_pool_used++;
150 if (obj_pool_used > obj_pool_max_used)
151 obj_pool_max_used = obj_pool_used;
152
153 obj_pool_free--;
154 if (obj_pool_free < obj_pool_min_free)
155 obj_pool_min_free = obj_pool_free;
156 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100157 raw_spin_unlock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700158
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700159 return obj;
160}
161
162/*
Thomas Gleixner337fff82009-03-16 10:04:53 +0100163 * workqueue function to free objects.
164 */
165static void free_obj_work(struct work_struct *work)
166{
167 struct debug_obj *obj;
168 unsigned long flags;
169
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100170 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100171 while (obj_pool_free > ODEBUG_POOL_SIZE) {
172 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
173 hlist_del(&obj->node);
174 obj_pool_free--;
175 /*
176 * We release pool_lock across kmem_cache_free() to
177 * avoid contention on pool_lock.
178 */
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100179 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100180 kmem_cache_free(obj_cache, obj);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100181 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100182 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100183 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100184}
185
186/*
187 * Put the object back into the pool and schedule work to free objects
188 * if necessary.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700189 */
190static void free_object(struct debug_obj *obj)
191{
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200192 unsigned long flags;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100193 int sched = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700194
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100195 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100196 /*
197 * schedule work when the pool is filled and the cache is
198 * initialized:
199 */
200 if (obj_pool_free > ODEBUG_POOL_SIZE && obj_cache)
Marcin Slusarz161b6ae2011-05-28 13:23:42 +0200201 sched = keventd_up() && !work_pending(&debug_obj_work);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100202 hlist_add_head(&obj->node, &obj_pool);
203 obj_pool_free++;
204 obj_pool_used--;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100205 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100206 if (sched)
207 schedule_work(&debug_obj_work);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700208}
209
210/*
211 * We run out of memory. That means we probably have tons of objects
212 * allocated.
213 */
214static void debug_objects_oom(void)
215{
216 struct debug_bucket *db = obj_hash;
217 struct hlist_node *node, *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200218 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700219 struct debug_obj *obj;
220 unsigned long flags;
221 int i;
222
223 printk(KERN_WARNING "ODEBUG: Out of memory. ODEBUG disabled\n");
224
225 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100226 raw_spin_lock_irqsave(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200227 hlist_move_list(&db->list, &freelist);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100228 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200229
230 /* Now free them */
231 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700232 hlist_del(&obj->node);
233 free_object(obj);
234 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700235 }
236}
237
238/*
239 * We use the pfn of the address for the hash. That way we can check
240 * for freed objects simply by checking the affected bucket.
241 */
242static struct debug_bucket *get_bucket(unsigned long addr)
243{
244 unsigned long hash;
245
246 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
247 return &obj_hash[hash];
248}
249
250static void debug_print_object(struct debug_obj *obj, char *msg)
251{
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100252 struct debug_obj_descr *descr = obj->descr;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700253 static int limit;
254
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100255 if (limit < 5 && descr != descr_test) {
256 void *hint = descr->debug_hint ?
257 descr->debug_hint(obj->object) : NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700258 limit++;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400259 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100260 "object type: %s hint: %pS\n",
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400261 msg, obj_states[obj->state], obj->astate,
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100262 descr->name, hint);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700263 }
264 debug_objects_warnings++;
265}
266
267/*
268 * Try to repair the damage, so we have a better chance to get useful
269 * debug output.
270 */
271static void
272debug_object_fixup(int (*fixup)(void *addr, enum debug_obj_state state),
273 void * addr, enum debug_obj_state state)
274{
275 if (fixup)
276 debug_objects_fixups += fixup(addr, state);
277}
278
279static void debug_object_is_on_stack(void *addr, int onstack)
280{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700281 int is_on_stack;
282 static int limit;
283
284 if (limit > 4)
285 return;
286
FUJITA Tomonori8b05c7e2008-07-23 21:26:53 -0700287 is_on_stack = object_is_on_stack(addr);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700288 if (is_on_stack == onstack)
289 return;
290
291 limit++;
292 if (is_on_stack)
293 printk(KERN_WARNING
294 "ODEBUG: object is on stack, but not annotated\n");
295 else
296 printk(KERN_WARNING
297 "ODEBUG: object is not on stack, but annotated\n");
298 WARN_ON(1);
299}
300
301static void
302__debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
303{
304 enum debug_obj_state state;
305 struct debug_bucket *db;
306 struct debug_obj *obj;
307 unsigned long flags;
308
Vegard Nossum50db04d2008-06-15 00:47:36 +0200309 fill_pool();
310
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700311 db = get_bucket((unsigned long) addr);
312
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100313 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700314
315 obj = lookup_object(addr, db);
316 if (!obj) {
317 obj = alloc_object(addr, db, descr);
318 if (!obj) {
319 debug_objects_enabled = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100320 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700321 debug_objects_oom();
322 return;
323 }
324 debug_object_is_on_stack(addr, onstack);
325 }
326
327 switch (obj->state) {
328 case ODEBUG_STATE_NONE:
329 case ODEBUG_STATE_INIT:
330 case ODEBUG_STATE_INACTIVE:
331 obj->state = ODEBUG_STATE_INIT;
332 break;
333
334 case ODEBUG_STATE_ACTIVE:
335 debug_print_object(obj, "init");
336 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100337 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700338 debug_object_fixup(descr->fixup_init, addr, state);
339 return;
340
341 case ODEBUG_STATE_DESTROYED:
342 debug_print_object(obj, "init");
343 break;
344 default:
345 break;
346 }
347
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100348 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700349}
350
351/**
352 * debug_object_init - debug checks when an object is initialized
353 * @addr: address of the object
354 * @descr: pointer to an object specific debug description structure
355 */
356void debug_object_init(void *addr, struct debug_obj_descr *descr)
357{
358 if (!debug_objects_enabled)
359 return;
360
361 __debug_object_init(addr, descr, 0);
362}
363
364/**
365 * debug_object_init_on_stack - debug checks when an object on stack is
366 * initialized
367 * @addr: address of the object
368 * @descr: pointer to an object specific debug description structure
369 */
370void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
371{
372 if (!debug_objects_enabled)
373 return;
374
375 __debug_object_init(addr, descr, 1);
376}
377
378/**
379 * debug_object_activate - debug checks when an object is activated
380 * @addr: address of the object
381 * @descr: pointer to an object specific debug description structure
382 */
383void debug_object_activate(void *addr, struct debug_obj_descr *descr)
384{
385 enum debug_obj_state state;
386 struct debug_bucket *db;
387 struct debug_obj *obj;
388 unsigned long flags;
389
390 if (!debug_objects_enabled)
391 return;
392
393 db = get_bucket((unsigned long) addr);
394
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100395 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700396
397 obj = lookup_object(addr, db);
398 if (obj) {
399 switch (obj->state) {
400 case ODEBUG_STATE_INIT:
401 case ODEBUG_STATE_INACTIVE:
402 obj->state = ODEBUG_STATE_ACTIVE;
403 break;
404
405 case ODEBUG_STATE_ACTIVE:
406 debug_print_object(obj, "activate");
407 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100408 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700409 debug_object_fixup(descr->fixup_activate, addr, state);
410 return;
411
412 case ODEBUG_STATE_DESTROYED:
413 debug_print_object(obj, "activate");
414 break;
415 default:
416 break;
417 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100418 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700419 return;
420 }
421
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100422 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700423 /*
424 * This happens when a static object is activated. We
425 * let the type specific code decide whether this is
426 * true or not.
427 */
428 debug_object_fixup(descr->fixup_activate, addr,
429 ODEBUG_STATE_NOTAVAILABLE);
430}
431
432/**
433 * debug_object_deactivate - debug checks when an object is deactivated
434 * @addr: address of the object
435 * @descr: pointer to an object specific debug description structure
436 */
437void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
438{
439 struct debug_bucket *db;
440 struct debug_obj *obj;
441 unsigned long flags;
442
443 if (!debug_objects_enabled)
444 return;
445
446 db = get_bucket((unsigned long) addr);
447
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100448 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700449
450 obj = lookup_object(addr, db);
451 if (obj) {
452 switch (obj->state) {
453 case ODEBUG_STATE_INIT:
454 case ODEBUG_STATE_INACTIVE:
455 case ODEBUG_STATE_ACTIVE:
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400456 if (!obj->astate)
457 obj->state = ODEBUG_STATE_INACTIVE;
458 else
459 debug_print_object(obj, "deactivate");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700460 break;
461
462 case ODEBUG_STATE_DESTROYED:
463 debug_print_object(obj, "deactivate");
464 break;
465 default:
466 break;
467 }
468 } else {
469 struct debug_obj o = { .object = addr,
470 .state = ODEBUG_STATE_NOTAVAILABLE,
471 .descr = descr };
472
473 debug_print_object(&o, "deactivate");
474 }
475
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100476 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700477}
478
479/**
480 * debug_object_destroy - debug checks when an object is destroyed
481 * @addr: address of the object
482 * @descr: pointer to an object specific debug description structure
483 */
484void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
485{
486 enum debug_obj_state state;
487 struct debug_bucket *db;
488 struct debug_obj *obj;
489 unsigned long flags;
490
491 if (!debug_objects_enabled)
492 return;
493
494 db = get_bucket((unsigned long) addr);
495
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100496 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700497
498 obj = lookup_object(addr, db);
499 if (!obj)
500 goto out_unlock;
501
502 switch (obj->state) {
503 case ODEBUG_STATE_NONE:
504 case ODEBUG_STATE_INIT:
505 case ODEBUG_STATE_INACTIVE:
506 obj->state = ODEBUG_STATE_DESTROYED;
507 break;
508 case ODEBUG_STATE_ACTIVE:
509 debug_print_object(obj, "destroy");
510 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100511 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700512 debug_object_fixup(descr->fixup_destroy, addr, state);
513 return;
514
515 case ODEBUG_STATE_DESTROYED:
516 debug_print_object(obj, "destroy");
517 break;
518 default:
519 break;
520 }
521out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100522 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700523}
524
525/**
526 * debug_object_free - debug checks when an object is freed
527 * @addr: address of the object
528 * @descr: pointer to an object specific debug description structure
529 */
530void debug_object_free(void *addr, struct debug_obj_descr *descr)
531{
532 enum debug_obj_state state;
533 struct debug_bucket *db;
534 struct debug_obj *obj;
535 unsigned long flags;
536
537 if (!debug_objects_enabled)
538 return;
539
540 db = get_bucket((unsigned long) addr);
541
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100542 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700543
544 obj = lookup_object(addr, db);
545 if (!obj)
546 goto out_unlock;
547
548 switch (obj->state) {
549 case ODEBUG_STATE_ACTIVE:
550 debug_print_object(obj, "free");
551 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100552 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700553 debug_object_fixup(descr->fixup_free, addr, state);
554 return;
555 default:
556 hlist_del(&obj->node);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100557 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700558 free_object(obj);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200559 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700560 }
561out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100562 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700563}
564
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400565/**
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700566 * debug_object_assert_init - debug checks when object should be init-ed
567 * @addr: address of the object
568 * @descr: pointer to an object specific debug description structure
569 */
570void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
571{
572 struct debug_bucket *db;
573 struct debug_obj *obj;
574 unsigned long flags;
575
576 if (!debug_objects_enabled)
577 return;
578
579 db = get_bucket((unsigned long) addr);
580
581 raw_spin_lock_irqsave(&db->lock, flags);
582
583 obj = lookup_object(addr, db);
584 if (!obj) {
585 raw_spin_unlock_irqrestore(&db->lock, flags);
586 /*
587 * Maybe the object is static. Let the type specific
588 * code decide what to do.
589 */
590 debug_object_fixup(descr->fixup_assert_init, addr,
591 ODEBUG_STATE_NOTAVAILABLE);
592 return;
593 }
594
595 raw_spin_unlock_irqrestore(&db->lock, flags);
596}
597
598/**
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400599 * debug_object_active_state - debug checks object usage state machine
600 * @addr: address of the object
601 * @descr: pointer to an object specific debug description structure
602 * @expect: expected state
603 * @next: state to move to if expected state is found
604 */
605void
606debug_object_active_state(void *addr, struct debug_obj_descr *descr,
607 unsigned int expect, unsigned int next)
608{
609 struct debug_bucket *db;
610 struct debug_obj *obj;
611 unsigned long flags;
612
613 if (!debug_objects_enabled)
614 return;
615
616 db = get_bucket((unsigned long) addr);
617
618 raw_spin_lock_irqsave(&db->lock, flags);
619
620 obj = lookup_object(addr, db);
621 if (obj) {
622 switch (obj->state) {
623 case ODEBUG_STATE_ACTIVE:
624 if (obj->astate == expect)
625 obj->astate = next;
626 else
627 debug_print_object(obj, "active_state");
628 break;
629
630 default:
631 debug_print_object(obj, "active_state");
632 break;
633 }
634 } else {
635 struct debug_obj o = { .object = addr,
636 .state = ODEBUG_STATE_NOTAVAILABLE,
637 .descr = descr };
638
639 debug_print_object(&o, "active_state");
640 }
641
642 raw_spin_unlock_irqrestore(&db->lock, flags);
643}
644
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700645#ifdef CONFIG_DEBUG_OBJECTS_FREE
646static void __debug_check_no_obj_freed(const void *address, unsigned long size)
647{
648 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
649 struct hlist_node *node, *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200650 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700651 struct debug_obj_descr *descr;
652 enum debug_obj_state state;
653 struct debug_bucket *db;
654 struct debug_obj *obj;
655 int cnt;
656
657 saddr = (unsigned long) address;
658 eaddr = saddr + size;
659 paddr = saddr & ODEBUG_CHUNK_MASK;
660 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
661 chunks >>= ODEBUG_CHUNK_SHIFT;
662
663 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
664 db = get_bucket(paddr);
665
666repeat:
667 cnt = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100668 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700669 hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
670 cnt++;
671 oaddr = (unsigned long) obj->object;
672 if (oaddr < saddr || oaddr >= eaddr)
673 continue;
674
675 switch (obj->state) {
676 case ODEBUG_STATE_ACTIVE:
677 debug_print_object(obj, "free");
678 descr = obj->descr;
679 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100680 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700681 debug_object_fixup(descr->fixup_free,
682 (void *) oaddr, state);
683 goto repeat;
684 default:
685 hlist_del(&obj->node);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200686 hlist_add_head(&obj->node, &freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700687 break;
688 }
689 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100690 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200691
692 /* Now free them */
693 hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
694 hlist_del(&obj->node);
695 free_object(obj);
696 }
697
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700698 if (cnt > debug_objects_maxchain)
699 debug_objects_maxchain = cnt;
700 }
701}
702
703void debug_check_no_obj_freed(const void *address, unsigned long size)
704{
705 if (debug_objects_enabled)
706 __debug_check_no_obj_freed(address, size);
707}
708#endif
709
710#ifdef CONFIG_DEBUG_FS
711
712static int debug_stats_show(struct seq_file *m, void *v)
713{
714 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
715 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
716 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
717 seq_printf(m, "pool_free :%d\n", obj_pool_free);
718 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
719 seq_printf(m, "pool_used :%d\n", obj_pool_used);
720 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
721 return 0;
722}
723
724static int debug_stats_open(struct inode *inode, struct file *filp)
725{
726 return single_open(filp, debug_stats_show, NULL);
727}
728
729static const struct file_operations debug_stats_fops = {
730 .open = debug_stats_open,
731 .read = seq_read,
732 .llseek = seq_lseek,
733 .release = single_release,
734};
735
736static int __init debug_objects_init_debugfs(void)
737{
738 struct dentry *dbgdir, *dbgstats;
739
740 if (!debug_objects_enabled)
741 return 0;
742
743 dbgdir = debugfs_create_dir("debug_objects", NULL);
744 if (!dbgdir)
745 return -ENOMEM;
746
747 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
748 &debug_stats_fops);
749 if (!dbgstats)
750 goto err;
751
752 return 0;
753
754err:
755 debugfs_remove(dbgdir);
756
757 return -ENOMEM;
758}
759__initcall(debug_objects_init_debugfs);
760
761#else
762static inline void debug_objects_init_debugfs(void) { }
763#endif
764
765#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
766
767/* Random data structure for the self test */
768struct self_test {
769 unsigned long dummy1[6];
770 int static_init;
771 unsigned long dummy2[3];
772};
773
774static __initdata struct debug_obj_descr descr_type_test;
775
776/*
777 * fixup_init is called when:
778 * - an active object is initialized
779 */
780static int __init fixup_init(void *addr, enum debug_obj_state state)
781{
782 struct self_test *obj = addr;
783
784 switch (state) {
785 case ODEBUG_STATE_ACTIVE:
786 debug_object_deactivate(obj, &descr_type_test);
787 debug_object_init(obj, &descr_type_test);
788 return 1;
789 default:
790 return 0;
791 }
792}
793
794/*
795 * fixup_activate is called when:
796 * - an active object is activated
797 * - an unknown object is activated (might be a statically initialized object)
798 */
799static int __init fixup_activate(void *addr, enum debug_obj_state state)
800{
801 struct self_test *obj = addr;
802
803 switch (state) {
804 case ODEBUG_STATE_NOTAVAILABLE:
805 if (obj->static_init == 1) {
806 debug_object_init(obj, &descr_type_test);
807 debug_object_activate(obj, &descr_type_test);
808 /*
809 * Real code should return 0 here ! This is
810 * not a fixup of some bad behaviour. We
811 * merily call the debug_init function to keep
812 * track of the object.
813 */
814 return 1;
815 } else {
816 /* Real code needs to emit a warning here */
817 }
818 return 0;
819
820 case ODEBUG_STATE_ACTIVE:
821 debug_object_deactivate(obj, &descr_type_test);
822 debug_object_activate(obj, &descr_type_test);
823 return 1;
824
825 default:
826 return 0;
827 }
828}
829
830/*
831 * fixup_destroy is called when:
832 * - an active object is destroyed
833 */
834static int __init fixup_destroy(void *addr, enum debug_obj_state state)
835{
836 struct self_test *obj = addr;
837
838 switch (state) {
839 case ODEBUG_STATE_ACTIVE:
840 debug_object_deactivate(obj, &descr_type_test);
841 debug_object_destroy(obj, &descr_type_test);
842 return 1;
843 default:
844 return 0;
845 }
846}
847
848/*
849 * fixup_free is called when:
850 * - an active object is freed
851 */
852static int __init fixup_free(void *addr, enum debug_obj_state state)
853{
854 struct self_test *obj = addr;
855
856 switch (state) {
857 case ODEBUG_STATE_ACTIVE:
858 debug_object_deactivate(obj, &descr_type_test);
859 debug_object_free(obj, &descr_type_test);
860 return 1;
861 default:
862 return 0;
863 }
864}
865
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +0100866static int __init
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700867check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
868{
869 struct debug_bucket *db;
870 struct debug_obj *obj;
871 unsigned long flags;
872 int res = -EINVAL;
873
874 db = get_bucket((unsigned long) addr);
875
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100876 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700877
878 obj = lookup_object(addr, db);
879 if (!obj && state != ODEBUG_STATE_NONE) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700880 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700881 goto out;
882 }
883 if (obj && obj->state != state) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700884 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700885 obj->state, state);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700886 goto out;
887 }
888 if (fixups != debug_objects_fixups) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700889 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700890 fixups, debug_objects_fixups);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700891 goto out;
892 }
893 if (warnings != debug_objects_warnings) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700894 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700895 warnings, debug_objects_warnings);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700896 goto out;
897 }
898 res = 0;
899out:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100900 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700901 if (res)
902 debug_objects_enabled = 0;
903 return res;
904}
905
906static __initdata struct debug_obj_descr descr_type_test = {
907 .name = "selftest",
908 .fixup_init = fixup_init,
909 .fixup_activate = fixup_activate,
910 .fixup_destroy = fixup_destroy,
911 .fixup_free = fixup_free,
912};
913
914static __initdata struct self_test obj = { .static_init = 0 };
915
916static void __init debug_objects_selftest(void)
917{
918 int fixups, oldfixups, warnings, oldwarnings;
919 unsigned long flags;
920
921 local_irq_save(flags);
922
923 fixups = oldfixups = debug_objects_fixups;
924 warnings = oldwarnings = debug_objects_warnings;
925 descr_test = &descr_type_test;
926
927 debug_object_init(&obj, &descr_type_test);
928 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
929 goto out;
930 debug_object_activate(&obj, &descr_type_test);
931 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
932 goto out;
933 debug_object_activate(&obj, &descr_type_test);
934 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
935 goto out;
936 debug_object_deactivate(&obj, &descr_type_test);
937 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
938 goto out;
939 debug_object_destroy(&obj, &descr_type_test);
940 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
941 goto out;
942 debug_object_init(&obj, &descr_type_test);
943 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
944 goto out;
945 debug_object_activate(&obj, &descr_type_test);
946 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
947 goto out;
948 debug_object_deactivate(&obj, &descr_type_test);
949 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
950 goto out;
951 debug_object_free(&obj, &descr_type_test);
952 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
953 goto out;
954
955 obj.static_init = 1;
956 debug_object_activate(&obj, &descr_type_test);
957 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, warnings))
958 goto out;
959 debug_object_init(&obj, &descr_type_test);
960 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
961 goto out;
962 debug_object_free(&obj, &descr_type_test);
963 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
964 goto out;
965
966#ifdef CONFIG_DEBUG_OBJECTS_FREE
967 debug_object_init(&obj, &descr_type_test);
968 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
969 goto out;
970 debug_object_activate(&obj, &descr_type_test);
971 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
972 goto out;
973 __debug_check_no_obj_freed(&obj, sizeof(obj));
974 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
975 goto out;
976#endif
977 printk(KERN_INFO "ODEBUG: selftest passed\n");
978
979out:
980 debug_objects_fixups = oldfixups;
981 debug_objects_warnings = oldwarnings;
982 descr_test = NULL;
983
984 local_irq_restore(flags);
985}
986#else
987static inline void debug_objects_selftest(void) { }
988#endif
989
990/*
991 * Called during early boot to initialize the hash buckets and link
992 * the static object pool objects into the poll list. After this call
993 * the object tracker is fully operational.
994 */
995void __init debug_objects_early_init(void)
996{
997 int i;
998
999 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001000 raw_spin_lock_init(&obj_hash[i].lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001001
1002 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1003 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1004}
1005
1006/*
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001007 * Convert the statically allocated objects to dynamic ones:
1008 */
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001009static int __init debug_objects_replace_static_objects(void)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001010{
1011 struct debug_bucket *db = obj_hash;
1012 struct hlist_node *node, *tmp;
1013 struct debug_obj *obj, *new;
1014 HLIST_HEAD(objects);
1015 int i, cnt = 0;
1016
1017 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1018 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1019 if (!obj)
1020 goto free;
1021 hlist_add_head(&obj->node, &objects);
1022 }
1023
1024 /*
1025 * When debug_objects_mem_init() is called we know that only
1026 * one CPU is up, so disabling interrupts is enough
1027 * protection. This avoids the lockdep hell of lock ordering.
1028 */
1029 local_irq_disable();
1030
1031 /* Remove the statically allocated objects from the pool */
1032 hlist_for_each_entry_safe(obj, node, tmp, &obj_pool, node)
1033 hlist_del(&obj->node);
1034 /* Move the allocated objects to the pool */
1035 hlist_move_list(&objects, &obj_pool);
1036
1037 /* Replace the active object references */
1038 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1039 hlist_move_list(&db->list, &objects);
1040
1041 hlist_for_each_entry(obj, node, &objects, node) {
1042 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1043 hlist_del(&new->node);
1044 /* copy object data */
1045 *new = *obj;
1046 hlist_add_head(&new->node, &db->list);
1047 cnt++;
1048 }
1049 }
1050
1051 printk(KERN_DEBUG "ODEBUG: %d of %d active objects replaced\n", cnt,
1052 obj_pool_used);
1053 local_irq_enable();
1054 return 0;
1055free:
1056 hlist_for_each_entry_safe(obj, node, tmp, &objects, node) {
1057 hlist_del(&obj->node);
1058 kmem_cache_free(obj_cache, obj);
1059 }
1060 return -ENOMEM;
1061}
1062
1063/*
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001064 * Called after the kmem_caches are functional to setup a dedicated
1065 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1066 * prevents that the debug code is called on kmem_cache_free() for the
1067 * debug tracker objects to avoid recursive calls.
1068 */
1069void __init debug_objects_mem_init(void)
1070{
1071 if (!debug_objects_enabled)
1072 return;
1073
1074 obj_cache = kmem_cache_create("debug_objects_cache",
1075 sizeof (struct debug_obj), 0,
1076 SLAB_DEBUG_OBJECTS, NULL);
1077
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001078 if (!obj_cache || debug_objects_replace_static_objects()) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001079 debug_objects_enabled = 0;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001080 if (obj_cache)
1081 kmem_cache_destroy(obj_cache);
1082 printk(KERN_WARNING "ODEBUG: out of memory.\n");
1083 } else
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001084 debug_objects_selftest();
1085}