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