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