2 * Generic infrastructure for lifetime debugging of objects.
4 * Started by Thomas Gleixner
6 * Copyright (C) 2008, Thomas Gleixner <tglx@linutronix.de>
8 * For licencing details see kernel-base/COPYING
11 #define pr_fmt(fmt) "ODEBUG: " fmt
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>
23 #define ODEBUG_HASH_BITS 14
24 #define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
26 #define ODEBUG_POOL_SIZE 1024
27 #define ODEBUG_POOL_MIN_LEVEL 256
28 #define ODEBUG_POOL_PERCPU_SIZE 64
30 #define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
31 #define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
32 #define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
35 struct hlist_head list;
40 * Debug object percpu free list
41 * Access is protected by disabling irq
43 struct debug_percpu_free {
44 struct hlist_head free_objs;
48 static DEFINE_PER_CPU(struct debug_percpu_free, percpu_obj_pool);
50 static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
52 static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
54 static DEFINE_RAW_SPINLOCK(pool_lock);
56 static HLIST_HEAD(obj_pool);
57 static HLIST_HEAD(obj_to_free);
60 * Because of the presence of percpu free pools, obj_pool_free will
61 * under-count those in the percpu free pools. Similarly, obj_pool_used
62 * will over-count those in the percpu free pools. Adjustments will be
63 * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
66 static int obj_pool_min_free = ODEBUG_POOL_SIZE;
67 static int obj_pool_free = ODEBUG_POOL_SIZE;
68 static int obj_pool_used;
69 static int obj_pool_max_used;
70 /* The number of objs on the global free list */
71 static int obj_nr_tofree;
73 static int debug_objects_maxchain __read_mostly;
74 static int __maybe_unused debug_objects_maxchecked __read_mostly;
75 static int debug_objects_fixups __read_mostly;
76 static int debug_objects_warnings __read_mostly;
77 static int debug_objects_enabled __read_mostly
78 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
79 static int debug_objects_pool_size __read_mostly
81 static int debug_objects_pool_min_level __read_mostly
82 = ODEBUG_POOL_MIN_LEVEL;
83 static struct debug_obj_descr *descr_test __read_mostly;
84 static struct kmem_cache *obj_cache __read_mostly;
87 * Track numbers of kmem_cache_alloc()/free() calls done.
89 static int debug_objects_allocated;
90 static int debug_objects_freed;
92 static void free_obj_work(struct work_struct *work);
93 static DECLARE_WORK(debug_obj_work, free_obj_work);
95 static int __init enable_object_debug(char *str)
97 debug_objects_enabled = 1;
101 static int __init disable_object_debug(char *str)
103 debug_objects_enabled = 0;
107 early_param("debug_objects", enable_object_debug);
108 early_param("no_debug_objects", disable_object_debug);
110 static const char *obj_states[ODEBUG_STATE_MAX] = {
111 [ODEBUG_STATE_NONE] = "none",
112 [ODEBUG_STATE_INIT] = "initialized",
113 [ODEBUG_STATE_INACTIVE] = "inactive",
114 [ODEBUG_STATE_ACTIVE] = "active",
115 [ODEBUG_STATE_DESTROYED] = "destroyed",
116 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
119 static void fill_pool(void)
121 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
122 struct debug_obj *new, *obj;
125 if (likely(obj_pool_free >= debug_objects_pool_min_level))
129 * Reuse objs from the global free list; they will be reinitialized
132 while (obj_nr_tofree && (obj_pool_free < obj_pool_min_free)) {
133 raw_spin_lock_irqsave(&pool_lock, flags);
135 * Recheck with the lock held as the worker thread might have
136 * won the race and freed the global free list already.
139 obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
140 hlist_del(&obj->node);
142 hlist_add_head(&obj->node, &obj_pool);
145 raw_spin_unlock_irqrestore(&pool_lock, flags);
148 if (unlikely(!obj_cache))
151 while (obj_pool_free < debug_objects_pool_min_level) {
153 new = kmem_cache_zalloc(obj_cache, gfp);
157 raw_spin_lock_irqsave(&pool_lock, flags);
158 hlist_add_head(&new->node, &obj_pool);
159 debug_objects_allocated++;
161 raw_spin_unlock_irqrestore(&pool_lock, flags);
166 * Lookup an object in the hash bucket.
168 static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
170 struct debug_obj *obj;
173 hlist_for_each_entry(obj, &b->list, node) {
175 if (obj->object == addr)
178 if (cnt > debug_objects_maxchain)
179 debug_objects_maxchain = cnt;
185 * Allocate a new object from the hlist
187 static struct debug_obj *__alloc_object(struct hlist_head *list)
189 struct debug_obj *obj = NULL;
192 obj = hlist_entry(list->first, typeof(*obj), node);
193 hlist_del(&obj->node);
199 static struct debug_obj *
200 alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
202 struct debug_percpu_free *percpu_pool;
203 struct debug_obj *obj;
205 if (likely(obj_cache)) {
206 percpu_pool = this_cpu_ptr(&percpu_obj_pool);
207 obj = __alloc_object(&percpu_pool->free_objs);
209 percpu_pool->obj_free--;
214 raw_spin_lock(&pool_lock);
215 obj = __alloc_object(&obj_pool);
218 if (obj_pool_used > obj_pool_max_used)
219 obj_pool_max_used = obj_pool_used;
222 if (obj_pool_free < obj_pool_min_free)
223 obj_pool_min_free = obj_pool_free;
225 raw_spin_unlock(&pool_lock);
231 obj->state = ODEBUG_STATE_NONE;
233 hlist_add_head(&obj->node, &b->list);
239 * workqueue function to free objects.
241 * To reduce contention on the global pool_lock, the actual freeing of
242 * debug objects will be delayed if the pool_lock is busy.
244 static void free_obj_work(struct work_struct *work)
246 struct hlist_node *tmp;
247 struct debug_obj *obj;
251 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
255 * The objs on the pool list might be allocated before the work is
256 * run, so recheck if pool list it full or not, if not fill pool
257 * list from the global free list
259 while (obj_nr_tofree && obj_pool_free < debug_objects_pool_size) {
260 obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
261 hlist_del(&obj->node);
262 hlist_add_head(&obj->node, &obj_pool);
268 * Pool list is already full and there are still objs on the free
269 * list. Move remaining free objs to a temporary list to free the
270 * memory outside the pool_lock held region.
273 hlist_move_list(&obj_to_free, &tofree);
274 debug_objects_freed += obj_nr_tofree;
277 raw_spin_unlock_irqrestore(&pool_lock, flags);
279 hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
280 hlist_del(&obj->node);
281 kmem_cache_free(obj_cache, obj);
285 static bool __free_object(struct debug_obj *obj)
289 struct debug_percpu_free *percpu_pool;
291 local_irq_save(flags);
293 * Try to free it into the percpu pool first.
295 percpu_pool = this_cpu_ptr(&percpu_obj_pool);
296 if (obj_cache && percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) {
297 hlist_add_head(&obj->node, &percpu_pool->free_objs);
298 percpu_pool->obj_free++;
299 local_irq_restore(flags);
303 raw_spin_lock(&pool_lock);
304 work = (obj_pool_free > debug_objects_pool_size) && obj_cache;
309 hlist_add_head(&obj->node, &obj_to_free);
312 hlist_add_head(&obj->node, &obj_pool);
314 raw_spin_unlock(&pool_lock);
315 local_irq_restore(flags);
320 * Put the object back into the pool and schedule work to free objects
323 static void free_object(struct debug_obj *obj)
325 if (__free_object(obj))
326 schedule_work(&debug_obj_work);
330 * We run out of memory. That means we probably have tons of objects
333 static void debug_objects_oom(void)
335 struct debug_bucket *db = obj_hash;
336 struct hlist_node *tmp;
337 HLIST_HEAD(freelist);
338 struct debug_obj *obj;
342 pr_warn("Out of memory. ODEBUG disabled\n");
344 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
345 raw_spin_lock_irqsave(&db->lock, flags);
346 hlist_move_list(&db->list, &freelist);
347 raw_spin_unlock_irqrestore(&db->lock, flags);
350 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
351 hlist_del(&obj->node);
358 * We use the pfn of the address for the hash. That way we can check
359 * for freed objects simply by checking the affected bucket.
361 static struct debug_bucket *get_bucket(unsigned long addr)
365 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
366 return &obj_hash[hash];
369 static void debug_print_object(struct debug_obj *obj, char *msg)
371 struct debug_obj_descr *descr = obj->descr;
375 * Don't report if lookup_object_or_alloc() by the current thread
376 * failed because lookup_object_or_alloc()/debug_objects_oom() by a
377 * concurrent thread turned off debug_objects_enabled and cleared
380 if (!debug_objects_enabled)
383 if (limit < 5 && descr != descr_test) {
384 void *hint = descr->debug_hint ?
385 descr->debug_hint(obj->object) : NULL;
387 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
388 "object type: %s hint: %pS\n",
389 msg, obj_states[obj->state], obj->astate,
392 debug_objects_warnings++;
396 * Try to repair the damage, so we have a better chance to get useful
400 debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
401 void * addr, enum debug_obj_state state)
403 if (fixup && fixup(addr, state)) {
404 debug_objects_fixups++;
410 static void debug_object_is_on_stack(void *addr, int onstack)
418 is_on_stack = object_is_on_stack(addr);
419 if (is_on_stack == onstack)
424 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
425 task_stack_page(current));
427 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
428 task_stack_page(current));
433 static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
434 struct debug_obj_descr *descr,
435 bool onstack, bool alloc_ifstatic)
437 struct debug_obj *obj = lookup_object(addr, b);
438 enum debug_obj_state state = ODEBUG_STATE_NONE;
444 * debug_object_init() unconditionally allocates untracked
445 * objects. It does not matter whether it is a static object or
448 * debug_object_assert_init() and debug_object_activate() allow
449 * allocation only if the descriptor callback confirms that the
450 * object is static and considered initialized. For non-static
451 * objects the allocation needs to be done from the fixup callback.
453 if (unlikely(alloc_ifstatic)) {
454 if (!descr->is_static_object || !descr->is_static_object(addr))
455 return ERR_PTR(-ENOENT);
456 /* Statically allocated objects are considered initialized */
457 state = ODEBUG_STATE_INIT;
460 obj = alloc_object(addr, b, descr);
463 debug_object_is_on_stack(addr, onstack);
467 /* Out of memory. Do the cleanup outside of the locked region */
468 debug_objects_enabled = 0;
472 static void debug_objects_fill_pool(void)
475 * On RT enabled kernels the pool refill must happen in preemptible
478 if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible())
483 __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
485 enum debug_obj_state state;
486 struct debug_bucket *db;
487 struct debug_obj *obj;
490 debug_objects_fill_pool();
492 db = get_bucket((unsigned long) addr);
494 raw_spin_lock_irqsave(&db->lock, flags);
496 obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
497 if (unlikely(!obj)) {
498 raw_spin_unlock_irqrestore(&db->lock, flags);
503 switch (obj->state) {
504 case ODEBUG_STATE_NONE:
505 case ODEBUG_STATE_INIT:
506 case ODEBUG_STATE_INACTIVE:
507 obj->state = ODEBUG_STATE_INIT;
510 case ODEBUG_STATE_ACTIVE:
512 raw_spin_unlock_irqrestore(&db->lock, flags);
513 debug_print_object(obj, "init");
514 debug_object_fixup(descr->fixup_init, addr, state);
517 case ODEBUG_STATE_DESTROYED:
518 raw_spin_unlock_irqrestore(&db->lock, flags);
519 debug_print_object(obj, "init");
525 raw_spin_unlock_irqrestore(&db->lock, flags);
529 * debug_object_init - debug checks when an object is initialized
530 * @addr: address of the object
531 * @descr: pointer to an object specific debug description structure
533 void debug_object_init(void *addr, struct debug_obj_descr *descr)
535 if (!debug_objects_enabled)
538 __debug_object_init(addr, descr, 0);
540 EXPORT_SYMBOL_GPL(debug_object_init);
543 * debug_object_init_on_stack - debug checks when an object on stack is
545 * @addr: address of the object
546 * @descr: pointer to an object specific debug description structure
548 void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
550 if (!debug_objects_enabled)
553 __debug_object_init(addr, descr, 1);
555 EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
558 * debug_object_activate - debug checks when an object is activated
559 * @addr: address of the object
560 * @descr: pointer to an object specific debug description structure
561 * Returns 0 for success, -EINVAL for check failed.
563 int debug_object_activate(void *addr, struct debug_obj_descr *descr)
565 struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
566 enum debug_obj_state state;
567 struct debug_bucket *db;
568 struct debug_obj *obj;
572 if (!debug_objects_enabled)
575 debug_objects_fill_pool();
577 db = get_bucket((unsigned long) addr);
579 raw_spin_lock_irqsave(&db->lock, flags);
581 obj = lookup_object_or_alloc(addr, db, descr, false, true);
582 if (likely(!IS_ERR_OR_NULL(obj))) {
583 bool print_object = false;
585 switch (obj->state) {
586 case ODEBUG_STATE_INIT:
587 case ODEBUG_STATE_INACTIVE:
588 obj->state = ODEBUG_STATE_ACTIVE;
592 case ODEBUG_STATE_ACTIVE:
594 raw_spin_unlock_irqrestore(&db->lock, flags);
595 debug_print_object(obj, "activate");
596 ret = debug_object_fixup(descr->fixup_activate, addr, state);
597 return ret ? 0 : -EINVAL;
599 case ODEBUG_STATE_DESTROYED:
607 raw_spin_unlock_irqrestore(&db->lock, flags);
609 debug_print_object(obj, "activate");
613 raw_spin_unlock_irqrestore(&db->lock, flags);
615 /* If NULL the allocation has hit OOM */
621 /* Object is neither static nor tracked. It's not initialized */
622 debug_print_object(&o, "activate");
623 ret = debug_object_fixup(descr->fixup_activate, addr, ODEBUG_STATE_NOTAVAILABLE);
624 return ret ? 0 : -EINVAL;
626 EXPORT_SYMBOL_GPL(debug_object_activate);
629 * debug_object_deactivate - debug checks when an object is deactivated
630 * @addr: address of the object
631 * @descr: pointer to an object specific debug description structure
633 void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
635 struct debug_bucket *db;
636 struct debug_obj *obj;
638 bool print_object = false;
640 if (!debug_objects_enabled)
643 db = get_bucket((unsigned long) addr);
645 raw_spin_lock_irqsave(&db->lock, flags);
647 obj = lookup_object(addr, db);
649 switch (obj->state) {
650 case ODEBUG_STATE_INIT:
651 case ODEBUG_STATE_INACTIVE:
652 case ODEBUG_STATE_ACTIVE:
654 obj->state = ODEBUG_STATE_INACTIVE;
659 case ODEBUG_STATE_DESTROYED:
667 raw_spin_unlock_irqrestore(&db->lock, flags);
669 struct debug_obj o = { .object = addr,
670 .state = ODEBUG_STATE_NOTAVAILABLE,
673 debug_print_object(&o, "deactivate");
674 } else if (print_object) {
675 debug_print_object(obj, "deactivate");
678 EXPORT_SYMBOL_GPL(debug_object_deactivate);
681 * debug_object_destroy - debug checks when an object is destroyed
682 * @addr: address of the object
683 * @descr: pointer to an object specific debug description structure
685 void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
687 enum debug_obj_state state;
688 struct debug_bucket *db;
689 struct debug_obj *obj;
691 bool print_object = false;
693 if (!debug_objects_enabled)
696 db = get_bucket((unsigned long) addr);
698 raw_spin_lock_irqsave(&db->lock, flags);
700 obj = lookup_object(addr, db);
704 switch (obj->state) {
705 case ODEBUG_STATE_NONE:
706 case ODEBUG_STATE_INIT:
707 case ODEBUG_STATE_INACTIVE:
708 obj->state = ODEBUG_STATE_DESTROYED;
710 case ODEBUG_STATE_ACTIVE:
712 raw_spin_unlock_irqrestore(&db->lock, flags);
713 debug_print_object(obj, "destroy");
714 debug_object_fixup(descr->fixup_destroy, addr, state);
717 case ODEBUG_STATE_DESTROYED:
724 raw_spin_unlock_irqrestore(&db->lock, flags);
726 debug_print_object(obj, "destroy");
728 EXPORT_SYMBOL_GPL(debug_object_destroy);
731 * debug_object_free - debug checks when an object is freed
732 * @addr: address of the object
733 * @descr: pointer to an object specific debug description structure
735 void debug_object_free(void *addr, struct debug_obj_descr *descr)
737 enum debug_obj_state state;
738 struct debug_bucket *db;
739 struct debug_obj *obj;
742 if (!debug_objects_enabled)
745 db = get_bucket((unsigned long) addr);
747 raw_spin_lock_irqsave(&db->lock, flags);
749 obj = lookup_object(addr, db);
753 switch (obj->state) {
754 case ODEBUG_STATE_ACTIVE:
756 raw_spin_unlock_irqrestore(&db->lock, flags);
757 debug_print_object(obj, "free");
758 debug_object_fixup(descr->fixup_free, addr, state);
761 hlist_del(&obj->node);
762 raw_spin_unlock_irqrestore(&db->lock, flags);
767 raw_spin_unlock_irqrestore(&db->lock, flags);
769 EXPORT_SYMBOL_GPL(debug_object_free);
772 * debug_object_assert_init - debug checks when object should be init-ed
773 * @addr: address of the object
774 * @descr: pointer to an object specific debug description structure
776 void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
778 struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
779 struct debug_bucket *db;
780 struct debug_obj *obj;
783 if (!debug_objects_enabled)
786 debug_objects_fill_pool();
788 db = get_bucket((unsigned long) addr);
790 raw_spin_lock_irqsave(&db->lock, flags);
791 obj = lookup_object_or_alloc(addr, db, descr, false, true);
792 raw_spin_unlock_irqrestore(&db->lock, flags);
793 if (likely(!IS_ERR_OR_NULL(obj)))
796 /* If NULL the allocation has hit OOM */
802 /* Object is neither tracked nor static. It's not initialized. */
803 debug_print_object(&o, "assert_init");
804 debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
806 EXPORT_SYMBOL_GPL(debug_object_assert_init);
809 * debug_object_active_state - debug checks object usage state machine
810 * @addr: address of the object
811 * @descr: pointer to an object specific debug description structure
812 * @expect: expected state
813 * @next: state to move to if expected state is found
816 debug_object_active_state(void *addr, struct debug_obj_descr *descr,
817 unsigned int expect, unsigned int next)
819 struct debug_bucket *db;
820 struct debug_obj *obj;
822 bool print_object = false;
824 if (!debug_objects_enabled)
827 db = get_bucket((unsigned long) addr);
829 raw_spin_lock_irqsave(&db->lock, flags);
831 obj = lookup_object(addr, db);
833 switch (obj->state) {
834 case ODEBUG_STATE_ACTIVE:
835 if (obj->astate == expect)
847 raw_spin_unlock_irqrestore(&db->lock, flags);
849 struct debug_obj o = { .object = addr,
850 .state = ODEBUG_STATE_NOTAVAILABLE,
853 debug_print_object(&o, "active_state");
854 } else if (print_object) {
855 debug_print_object(obj, "active_state");
858 EXPORT_SYMBOL_GPL(debug_object_active_state);
860 #ifdef CONFIG_DEBUG_OBJECTS_FREE
861 static void __debug_check_no_obj_freed(const void *address, unsigned long size)
863 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
864 struct debug_obj_descr *descr;
865 enum debug_obj_state state;
866 struct debug_bucket *db;
867 struct hlist_node *tmp;
868 struct debug_obj *obj;
869 int cnt, objs_checked = 0;
872 saddr = (unsigned long) address;
873 eaddr = saddr + size;
874 paddr = saddr & ODEBUG_CHUNK_MASK;
875 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
876 chunks >>= ODEBUG_CHUNK_SHIFT;
878 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
879 db = get_bucket(paddr);
883 raw_spin_lock_irqsave(&db->lock, flags);
884 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
886 oaddr = (unsigned long) obj->object;
887 if (oaddr < saddr || oaddr >= eaddr)
890 switch (obj->state) {
891 case ODEBUG_STATE_ACTIVE:
894 raw_spin_unlock_irqrestore(&db->lock, flags);
895 debug_print_object(obj, "free");
896 debug_object_fixup(descr->fixup_free,
897 (void *) oaddr, state);
900 hlist_del(&obj->node);
901 work |= __free_object(obj);
905 raw_spin_unlock_irqrestore(&db->lock, flags);
907 if (cnt > debug_objects_maxchain)
908 debug_objects_maxchain = cnt;
913 if (objs_checked > debug_objects_maxchecked)
914 debug_objects_maxchecked = objs_checked;
916 /* Schedule work to actually kmem_cache_free() objects */
918 schedule_work(&debug_obj_work);
921 void debug_check_no_obj_freed(const void *address, unsigned long size)
923 if (debug_objects_enabled)
924 __debug_check_no_obj_freed(address, size);
928 #ifdef CONFIG_DEBUG_FS
930 static int debug_stats_show(struct seq_file *m, void *v)
932 int cpu, obj_percpu_free = 0;
934 for_each_possible_cpu(cpu)
935 obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu);
937 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
938 seq_printf(m, "max_checked :%d\n", debug_objects_maxchecked);
939 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
940 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
941 seq_printf(m, "pool_free :%d\n", obj_pool_free + obj_percpu_free);
942 seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
943 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
944 seq_printf(m, "pool_used :%d\n", obj_pool_used - obj_percpu_free);
945 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
946 seq_printf(m, "on_free_list :%d\n", obj_nr_tofree);
947 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
948 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
952 static int debug_stats_open(struct inode *inode, struct file *filp)
954 return single_open(filp, debug_stats_show, NULL);
957 static const struct file_operations debug_stats_fops = {
958 .open = debug_stats_open,
961 .release = single_release,
964 static int __init debug_objects_init_debugfs(void)
966 struct dentry *dbgdir, *dbgstats;
968 if (!debug_objects_enabled)
971 dbgdir = debugfs_create_dir("debug_objects", NULL);
975 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
983 debugfs_remove(dbgdir);
987 __initcall(debug_objects_init_debugfs);
990 static inline void debug_objects_init_debugfs(void) { }
993 #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
995 /* Random data structure for the self test */
997 unsigned long dummy1[6];
999 unsigned long dummy2[3];
1002 static __initdata struct debug_obj_descr descr_type_test;
1004 static bool __init is_static_object(void *addr)
1006 struct self_test *obj = addr;
1008 return obj->static_init;
1012 * fixup_init is called when:
1013 * - an active object is initialized
1015 static bool __init fixup_init(void *addr, enum debug_obj_state state)
1017 struct self_test *obj = addr;
1020 case ODEBUG_STATE_ACTIVE:
1021 debug_object_deactivate(obj, &descr_type_test);
1022 debug_object_init(obj, &descr_type_test);
1030 * fixup_activate is called when:
1031 * - an active object is activated
1032 * - an unknown non-static object is activated
1034 static bool __init fixup_activate(void *addr, enum debug_obj_state state)
1036 struct self_test *obj = addr;
1039 case ODEBUG_STATE_NOTAVAILABLE:
1041 case ODEBUG_STATE_ACTIVE:
1042 debug_object_deactivate(obj, &descr_type_test);
1043 debug_object_activate(obj, &descr_type_test);
1052 * fixup_destroy is called when:
1053 * - an active object is destroyed
1055 static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
1057 struct self_test *obj = addr;
1060 case ODEBUG_STATE_ACTIVE:
1061 debug_object_deactivate(obj, &descr_type_test);
1062 debug_object_destroy(obj, &descr_type_test);
1070 * fixup_free is called when:
1071 * - an active object is freed
1073 static bool __init fixup_free(void *addr, enum debug_obj_state state)
1075 struct self_test *obj = addr;
1078 case ODEBUG_STATE_ACTIVE:
1079 debug_object_deactivate(obj, &descr_type_test);
1080 debug_object_free(obj, &descr_type_test);
1088 check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
1090 struct debug_bucket *db;
1091 struct debug_obj *obj;
1092 unsigned long flags;
1095 db = get_bucket((unsigned long) addr);
1097 raw_spin_lock_irqsave(&db->lock, flags);
1099 obj = lookup_object(addr, db);
1100 if (!obj && state != ODEBUG_STATE_NONE) {
1101 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
1104 if (obj && obj->state != state) {
1105 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
1109 if (fixups != debug_objects_fixups) {
1110 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
1111 fixups, debug_objects_fixups);
1114 if (warnings != debug_objects_warnings) {
1115 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
1116 warnings, debug_objects_warnings);
1121 raw_spin_unlock_irqrestore(&db->lock, flags);
1123 debug_objects_enabled = 0;
1127 static __initdata struct debug_obj_descr descr_type_test = {
1129 .is_static_object = is_static_object,
1130 .fixup_init = fixup_init,
1131 .fixup_activate = fixup_activate,
1132 .fixup_destroy = fixup_destroy,
1133 .fixup_free = fixup_free,
1136 static __initdata struct self_test obj = { .static_init = 0 };
1138 static void __init debug_objects_selftest(void)
1140 int fixups, oldfixups, warnings, oldwarnings;
1141 unsigned long flags;
1143 local_irq_save(flags);
1145 fixups = oldfixups = debug_objects_fixups;
1146 warnings = oldwarnings = debug_objects_warnings;
1147 descr_test = &descr_type_test;
1149 debug_object_init(&obj, &descr_type_test);
1150 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1152 debug_object_activate(&obj, &descr_type_test);
1153 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1155 debug_object_activate(&obj, &descr_type_test);
1156 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1158 debug_object_deactivate(&obj, &descr_type_test);
1159 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1161 debug_object_destroy(&obj, &descr_type_test);
1162 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1164 debug_object_init(&obj, &descr_type_test);
1165 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1167 debug_object_activate(&obj, &descr_type_test);
1168 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1170 debug_object_deactivate(&obj, &descr_type_test);
1171 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1173 debug_object_free(&obj, &descr_type_test);
1174 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1177 obj.static_init = 1;
1178 debug_object_activate(&obj, &descr_type_test);
1179 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1181 debug_object_init(&obj, &descr_type_test);
1182 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1184 debug_object_free(&obj, &descr_type_test);
1185 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1188 #ifdef CONFIG_DEBUG_OBJECTS_FREE
1189 debug_object_init(&obj, &descr_type_test);
1190 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1192 debug_object_activate(&obj, &descr_type_test);
1193 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1195 __debug_check_no_obj_freed(&obj, sizeof(obj));
1196 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1199 pr_info("selftest passed\n");
1202 debug_objects_fixups = oldfixups;
1203 debug_objects_warnings = oldwarnings;
1206 local_irq_restore(flags);
1209 static inline void debug_objects_selftest(void) { }
1213 * Called during early boot to initialize the hash buckets and link
1214 * the static object pool objects into the poll list. After this call
1215 * the object tracker is fully operational.
1217 void __init debug_objects_early_init(void)
1221 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1222 raw_spin_lock_init(&obj_hash[i].lock);
1224 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1225 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1229 * Convert the statically allocated objects to dynamic ones:
1231 static int __init debug_objects_replace_static_objects(void)
1233 struct debug_bucket *db = obj_hash;
1234 struct hlist_node *tmp;
1235 struct debug_obj *obj, *new;
1236 HLIST_HEAD(objects);
1239 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1240 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1243 hlist_add_head(&obj->node, &objects);
1247 * When debug_objects_mem_init() is called we know that only
1248 * one CPU is up, so disabling interrupts is enough
1249 * protection. This avoids the lockdep hell of lock ordering.
1251 local_irq_disable();
1253 /* Remove the statically allocated objects from the pool */
1254 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
1255 hlist_del(&obj->node);
1256 /* Move the allocated objects to the pool */
1257 hlist_move_list(&objects, &obj_pool);
1259 /* Replace the active object references */
1260 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1261 hlist_move_list(&db->list, &objects);
1263 hlist_for_each_entry(obj, &objects, node) {
1264 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1265 hlist_del(&new->node);
1266 /* copy object data */
1268 hlist_add_head(&new->node, &db->list);
1274 pr_debug("%d of %d active objects replaced\n",
1275 cnt, obj_pool_used);
1278 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
1279 hlist_del(&obj->node);
1280 kmem_cache_free(obj_cache, obj);
1286 * Called after the kmem_caches are functional to setup a dedicated
1287 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1288 * prevents that the debug code is called on kmem_cache_free() for the
1289 * debug tracker objects to avoid recursive calls.
1291 void __init debug_objects_mem_init(void)
1295 if (!debug_objects_enabled)
1299 * Initialize the percpu object pools
1301 * Initialization is not strictly necessary, but was done for
1304 for_each_possible_cpu(cpu)
1305 INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool.free_objs, cpu));
1307 obj_cache = kmem_cache_create("debug_objects_cache",
1308 sizeof (struct debug_obj), 0,
1309 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
1312 if (!obj_cache || debug_objects_replace_static_objects()) {
1313 debug_objects_enabled = 0;
1314 kmem_cache_destroy(obj_cache);
1315 pr_warn("out of memory.\n");
1317 debug_objects_selftest();