Blame view

lib/debugobjects.c 21.3 KB
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  /*
   * Generic infrastructure for lifetime debugging of objects.
   *
   * Started by Thomas Gleixner
   *
   * Copyright (C) 2008, Thomas Gleixner <tglx@linutronix.de>
   *
   * For licencing details see kernel-base/COPYING
   */
  #include <linux/debugobjects.h>
  #include <linux/interrupt.h>
  #include <linux/seq_file.h>
  #include <linux/debugfs.h>
  #include <linux/hash.h>
  
  #define ODEBUG_HASH_BITS	14
  #define ODEBUG_HASH_SIZE	(1 << ODEBUG_HASH_BITS)
  
  #define ODEBUG_POOL_SIZE	512
  #define ODEBUG_POOL_MIN_LEVEL	256
  
  #define ODEBUG_CHUNK_SHIFT	PAGE_SHIFT
  #define ODEBUG_CHUNK_SIZE	(1 << ODEBUG_CHUNK_SHIFT)
  #define ODEBUG_CHUNK_MASK	(~(ODEBUG_CHUNK_SIZE - 1))
  
  struct debug_bucket {
  	struct hlist_head	list;
  	spinlock_t		lock;
  };
  
  static struct debug_bucket	obj_hash[ODEBUG_HASH_SIZE];
  
  static struct debug_obj		obj_static_pool[ODEBUG_POOL_SIZE];
  
  static DEFINE_SPINLOCK(pool_lock);
  
  static HLIST_HEAD(obj_pool);
  
  static int			obj_pool_min_free = ODEBUG_POOL_SIZE;
  static int			obj_pool_free = ODEBUG_POOL_SIZE;
  static int			obj_pool_used;
  static int			obj_pool_max_used;
  static struct kmem_cache	*obj_cache;
  
  static int			debug_objects_maxchain __read_mostly;
  static int			debug_objects_fixups __read_mostly;
  static int			debug_objects_warnings __read_mostly;
  static int			debug_objects_enabled __read_mostly;
  static struct debug_obj_descr	*descr_test  __read_mostly;
  
  static int __init enable_object_debug(char *str)
  {
  	debug_objects_enabled = 1;
  	return 0;
  }
  early_param("debug_objects", enable_object_debug);
  
  static const char *obj_states[ODEBUG_STATE_MAX] = {
  	[ODEBUG_STATE_NONE]		= "none",
  	[ODEBUG_STATE_INIT]		= "initialized",
  	[ODEBUG_STATE_INACTIVE]		= "inactive",
  	[ODEBUG_STATE_ACTIVE]		= "active",
  	[ODEBUG_STATE_DESTROYED]	= "destroyed",
  	[ODEBUG_STATE_NOTAVAILABLE]	= "not available",
  };
  
  static int fill_pool(void)
  {
  	gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
  	struct debug_obj *new;
50db04dd9   Vegard Nossum   debugobjects: fix...
71
  	unsigned long flags;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
72
73
74
75
76
77
78
79
80
81
82
83
  
  	if (likely(obj_pool_free >= ODEBUG_POOL_MIN_LEVEL))
  		return obj_pool_free;
  
  	if (unlikely(!obj_cache))
  		return obj_pool_free;
  
  	while (obj_pool_free < ODEBUG_POOL_MIN_LEVEL) {
  
  		new = kmem_cache_zalloc(obj_cache, gfp);
  		if (!new)
  			return obj_pool_free;
50db04dd9   Vegard Nossum   debugobjects: fix...
84
  		spin_lock_irqsave(&pool_lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
85
86
  		hlist_add_head(&new->node, &obj_pool);
  		obj_pool_free++;
50db04dd9   Vegard Nossum   debugobjects: fix...
87
  		spin_unlock_irqrestore(&pool_lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
  	}
  	return obj_pool_free;
  }
  
  /*
   * Lookup an object in the hash bucket.
   */
  static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
  {
  	struct hlist_node *node;
  	struct debug_obj *obj;
  	int cnt = 0;
  
  	hlist_for_each_entry(obj, node, &b->list, node) {
  		cnt++;
  		if (obj->object == addr)
  			return obj;
  	}
  	if (cnt > debug_objects_maxchain)
  		debug_objects_maxchain = cnt;
  
  	return NULL;
  }
  
  /*
50db04dd9   Vegard Nossum   debugobjects: fix...
113
   * Allocate a new object. If the pool is empty, switch off the debugger.
673d62cc5   Vegard Nossum   debugobjects: fix...
114
   * Must be called with interrupts disabled.
3ac7fe5a4   Thomas Gleixner   infrastructure to...
115
116
117
118
119
   */
  static struct debug_obj *
  alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
  {
  	struct debug_obj *obj = NULL;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
120

3ac7fe5a4   Thomas Gleixner   infrastructure to...
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  	spin_lock(&pool_lock);
  	if (obj_pool.first) {
  		obj	    = hlist_entry(obj_pool.first, typeof(*obj), node);
  
  		obj->object = addr;
  		obj->descr  = descr;
  		obj->state  = ODEBUG_STATE_NONE;
  		hlist_del(&obj->node);
  
  		hlist_add_head(&obj->node, &b->list);
  
  		obj_pool_used++;
  		if (obj_pool_used > obj_pool_max_used)
  			obj_pool_max_used = obj_pool_used;
  
  		obj_pool_free--;
  		if (obj_pool_free < obj_pool_min_free)
  			obj_pool_min_free = obj_pool_free;
  	}
  	spin_unlock(&pool_lock);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
141
142
143
144
145
146
147
148
149
  	return obj;
  }
  
  /*
   * Put the object back into the pool or give it back to kmem_cache:
   */
  static void free_object(struct debug_obj *obj)
  {
  	unsigned long idx = (unsigned long)(obj - obj_static_pool);
673d62cc5   Vegard Nossum   debugobjects: fix...
150
  	unsigned long flags;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
151
152
  
  	if (obj_pool_free < ODEBUG_POOL_SIZE || idx < ODEBUG_POOL_SIZE) {
673d62cc5   Vegard Nossum   debugobjects: fix...
153
  		spin_lock_irqsave(&pool_lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
154
155
156
  		hlist_add_head(&obj->node, &obj_pool);
  		obj_pool_free++;
  		obj_pool_used--;
673d62cc5   Vegard Nossum   debugobjects: fix...
157
  		spin_unlock_irqrestore(&pool_lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
158
  	} else {
673d62cc5   Vegard Nossum   debugobjects: fix...
159
  		spin_lock_irqsave(&pool_lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
160
  		obj_pool_used--;
673d62cc5   Vegard Nossum   debugobjects: fix...
161
  		spin_unlock_irqrestore(&pool_lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
162
163
164
165
166
167
168
169
170
171
172
173
  		kmem_cache_free(obj_cache, obj);
  	}
  }
  
  /*
   * We run out of memory. That means we probably have tons of objects
   * allocated.
   */
  static void debug_objects_oom(void)
  {
  	struct debug_bucket *db = obj_hash;
  	struct hlist_node *node, *tmp;
673d62cc5   Vegard Nossum   debugobjects: fix...
174
  	HLIST_HEAD(freelist);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
175
176
177
178
179
180
181
182
183
  	struct debug_obj *obj;
  	unsigned long flags;
  	int i;
  
  	printk(KERN_WARNING "ODEBUG: Out of memory. ODEBUG disabled
  ");
  
  	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
  		spin_lock_irqsave(&db->lock, flags);
673d62cc5   Vegard Nossum   debugobjects: fix...
184
185
186
187
188
  		hlist_move_list(&db->list, &freelist);
  		spin_unlock_irqrestore(&db->lock, flags);
  
  		/* Now free them */
  		hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
189
190
191
  			hlist_del(&obj->node);
  			free_object(obj);
  		}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
  	}
  }
  
  /*
   * We use the pfn of the address for the hash. That way we can check
   * for freed objects simply by checking the affected bucket.
   */
  static struct debug_bucket *get_bucket(unsigned long addr)
  {
  	unsigned long hash;
  
  	hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
  	return &obj_hash[hash];
  }
  
  static void debug_print_object(struct debug_obj *obj, char *msg)
  {
  	static int limit;
  
  	if (limit < 5 && obj->descr != descr_test) {
  		limit++;
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
213
214
  		WARN(1, KERN_ERR "ODEBUG: %s %s object type: %s
  ", msg,
3ac7fe5a4   Thomas Gleixner   infrastructure to...
215
  		       obj_states[obj->state], obj->descr->name);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  	}
  	debug_objects_warnings++;
  }
  
  /*
   * Try to repair the damage, so we have a better chance to get useful
   * debug output.
   */
  static void
  debug_object_fixup(int (*fixup)(void *addr, enum debug_obj_state state),
  		   void * addr, enum debug_obj_state state)
  {
  	if (fixup)
  		debug_objects_fixups += fixup(addr, state);
  }
  
  static void debug_object_is_on_stack(void *addr, int onstack)
  {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
234
235
236
237
238
  	int is_on_stack;
  	static int limit;
  
  	if (limit > 4)
  		return;
8b05c7e6e   FUJITA Tomonori   add a helper func...
239
  	is_on_stack = object_is_on_stack(addr);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
  	if (is_on_stack == onstack)
  		return;
  
  	limit++;
  	if (is_on_stack)
  		printk(KERN_WARNING
  		       "ODEBUG: object is on stack, but not annotated
  ");
  	else
  		printk(KERN_WARNING
  		       "ODEBUG: object is not on stack, but annotated
  ");
  	WARN_ON(1);
  }
  
  static void
  __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
  {
  	enum debug_obj_state state;
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
50db04dd9   Vegard Nossum   debugobjects: fix...
262
  	fill_pool();
3ac7fe5a4   Thomas Gleixner   infrastructure to...
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
  	db = get_bucket((unsigned long) addr);
  
  	spin_lock_irqsave(&db->lock, flags);
  
  	obj = lookup_object(addr, db);
  	if (!obj) {
  		obj = alloc_object(addr, db, descr);
  		if (!obj) {
  			debug_objects_enabled = 0;
  			spin_unlock_irqrestore(&db->lock, flags);
  			debug_objects_oom();
  			return;
  		}
  		debug_object_is_on_stack(addr, onstack);
  	}
  
  	switch (obj->state) {
  	case ODEBUG_STATE_NONE:
  	case ODEBUG_STATE_INIT:
  	case ODEBUG_STATE_INACTIVE:
  		obj->state = ODEBUG_STATE_INIT;
  		break;
  
  	case ODEBUG_STATE_ACTIVE:
  		debug_print_object(obj, "init");
  		state = obj->state;
  		spin_unlock_irqrestore(&db->lock, flags);
  		debug_object_fixup(descr->fixup_init, addr, state);
  		return;
  
  	case ODEBUG_STATE_DESTROYED:
  		debug_print_object(obj, "init");
  		break;
  	default:
  		break;
  	}
  
  	spin_unlock_irqrestore(&db->lock, flags);
  }
  
  /**
   * debug_object_init - debug checks when an object is initialized
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
   */
  void debug_object_init(void *addr, struct debug_obj_descr *descr)
  {
  	if (!debug_objects_enabled)
  		return;
  
  	__debug_object_init(addr, descr, 0);
  }
  
  /**
   * debug_object_init_on_stack - debug checks when an object on stack is
   *				initialized
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
   */
  void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
  {
  	if (!debug_objects_enabled)
  		return;
  
  	__debug_object_init(addr, descr, 1);
  }
  
  /**
   * debug_object_activate - debug checks when an object is activated
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
   */
  void debug_object_activate(void *addr, struct debug_obj_descr *descr)
  {
  	enum debug_obj_state state;
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
  
  	if (!debug_objects_enabled)
  		return;
  
  	db = get_bucket((unsigned long) addr);
  
  	spin_lock_irqsave(&db->lock, flags);
  
  	obj = lookup_object(addr, db);
  	if (obj) {
  		switch (obj->state) {
  		case ODEBUG_STATE_INIT:
  		case ODEBUG_STATE_INACTIVE:
  			obj->state = ODEBUG_STATE_ACTIVE;
  			break;
  
  		case ODEBUG_STATE_ACTIVE:
  			debug_print_object(obj, "activate");
  			state = obj->state;
  			spin_unlock_irqrestore(&db->lock, flags);
  			debug_object_fixup(descr->fixup_activate, addr, state);
  			return;
  
  		case ODEBUG_STATE_DESTROYED:
  			debug_print_object(obj, "activate");
  			break;
  		default:
  			break;
  		}
  		spin_unlock_irqrestore(&db->lock, flags);
  		return;
  	}
  
  	spin_unlock_irqrestore(&db->lock, flags);
  	/*
  	 * This happens when a static object is activated. We
  	 * let the type specific code decide whether this is
  	 * true or not.
  	 */
  	debug_object_fixup(descr->fixup_activate, addr,
  			   ODEBUG_STATE_NOTAVAILABLE);
  }
  
  /**
   * debug_object_deactivate - debug checks when an object is deactivated
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
   */
  void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
  {
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
  
  	if (!debug_objects_enabled)
  		return;
  
  	db = get_bucket((unsigned long) addr);
  
  	spin_lock_irqsave(&db->lock, flags);
  
  	obj = lookup_object(addr, db);
  	if (obj) {
  		switch (obj->state) {
  		case ODEBUG_STATE_INIT:
  		case ODEBUG_STATE_INACTIVE:
  		case ODEBUG_STATE_ACTIVE:
  			obj->state = ODEBUG_STATE_INACTIVE;
  			break;
  
  		case ODEBUG_STATE_DESTROYED:
  			debug_print_object(obj, "deactivate");
  			break;
  		default:
  			break;
  		}
  	} else {
  		struct debug_obj o = { .object = addr,
  				       .state = ODEBUG_STATE_NOTAVAILABLE,
  				       .descr = descr };
  
  		debug_print_object(&o, "deactivate");
  	}
  
  	spin_unlock_irqrestore(&db->lock, flags);
  }
  
  /**
   * debug_object_destroy - debug checks when an object is destroyed
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
   */
  void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
  {
  	enum debug_obj_state state;
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
  
  	if (!debug_objects_enabled)
  		return;
  
  	db = get_bucket((unsigned long) addr);
  
  	spin_lock_irqsave(&db->lock, flags);
  
  	obj = lookup_object(addr, db);
  	if (!obj)
  		goto out_unlock;
  
  	switch (obj->state) {
  	case ODEBUG_STATE_NONE:
  	case ODEBUG_STATE_INIT:
  	case ODEBUG_STATE_INACTIVE:
  		obj->state = ODEBUG_STATE_DESTROYED;
  		break;
  	case ODEBUG_STATE_ACTIVE:
  		debug_print_object(obj, "destroy");
  		state = obj->state;
  		spin_unlock_irqrestore(&db->lock, flags);
  		debug_object_fixup(descr->fixup_destroy, addr, state);
  		return;
  
  	case ODEBUG_STATE_DESTROYED:
  		debug_print_object(obj, "destroy");
  		break;
  	default:
  		break;
  	}
  out_unlock:
  	spin_unlock_irqrestore(&db->lock, flags);
  }
  
  /**
   * debug_object_free - debug checks when an object is freed
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
   */
  void debug_object_free(void *addr, struct debug_obj_descr *descr)
  {
  	enum debug_obj_state state;
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
  
  	if (!debug_objects_enabled)
  		return;
  
  	db = get_bucket((unsigned long) addr);
  
  	spin_lock_irqsave(&db->lock, flags);
  
  	obj = lookup_object(addr, db);
  	if (!obj)
  		goto out_unlock;
  
  	switch (obj->state) {
  	case ODEBUG_STATE_ACTIVE:
  		debug_print_object(obj, "free");
  		state = obj->state;
  		spin_unlock_irqrestore(&db->lock, flags);
  		debug_object_fixup(descr->fixup_free, addr, state);
  		return;
  	default:
  		hlist_del(&obj->node);
673d62cc5   Vegard Nossum   debugobjects: fix...
506
  		spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
507
  		free_object(obj);
673d62cc5   Vegard Nossum   debugobjects: fix...
508
  		return;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
509
510
511
512
513
514
515
516
517
518
  	}
  out_unlock:
  	spin_unlock_irqrestore(&db->lock, flags);
  }
  
  #ifdef CONFIG_DEBUG_OBJECTS_FREE
  static void __debug_check_no_obj_freed(const void *address, unsigned long size)
  {
  	unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
  	struct hlist_node *node, *tmp;
673d62cc5   Vegard Nossum   debugobjects: fix...
519
  	HLIST_HEAD(freelist);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
  	struct debug_obj_descr *descr;
  	enum debug_obj_state state;
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	int cnt;
  
  	saddr = (unsigned long) address;
  	eaddr = saddr + size;
  	paddr = saddr & ODEBUG_CHUNK_MASK;
  	chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
  	chunks >>= ODEBUG_CHUNK_SHIFT;
  
  	for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
  		db = get_bucket(paddr);
  
  repeat:
  		cnt = 0;
  		spin_lock_irqsave(&db->lock, flags);
  		hlist_for_each_entry_safe(obj, node, tmp, &db->list, node) {
  			cnt++;
  			oaddr = (unsigned long) obj->object;
  			if (oaddr < saddr || oaddr >= eaddr)
  				continue;
  
  			switch (obj->state) {
  			case ODEBUG_STATE_ACTIVE:
  				debug_print_object(obj, "free");
  				descr = obj->descr;
  				state = obj->state;
  				spin_unlock_irqrestore(&db->lock, flags);
  				debug_object_fixup(descr->fixup_free,
  						   (void *) oaddr, state);
  				goto repeat;
  			default:
  				hlist_del(&obj->node);
673d62cc5   Vegard Nossum   debugobjects: fix...
555
  				hlist_add_head(&obj->node, &freelist);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
556
557
558
559
  				break;
  			}
  		}
  		spin_unlock_irqrestore(&db->lock, flags);
673d62cc5   Vegard Nossum   debugobjects: fix...
560
561
562
563
564
565
  
  		/* Now free them */
  		hlist_for_each_entry_safe(obj, node, tmp, &freelist, node) {
  			hlist_del(&obj->node);
  			free_object(obj);
  		}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
  		if (cnt > debug_objects_maxchain)
  			debug_objects_maxchain = cnt;
  	}
  }
  
  void debug_check_no_obj_freed(const void *address, unsigned long size)
  {
  	if (debug_objects_enabled)
  		__debug_check_no_obj_freed(address, size);
  }
  #endif
  
  #ifdef CONFIG_DEBUG_FS
  
  static int debug_stats_show(struct seq_file *m, void *v)
  {
  	seq_printf(m, "max_chain     :%d
  ", debug_objects_maxchain);
  	seq_printf(m, "warnings      :%d
  ", debug_objects_warnings);
  	seq_printf(m, "fixups        :%d
  ", debug_objects_fixups);
  	seq_printf(m, "pool_free     :%d
  ", obj_pool_free);
  	seq_printf(m, "pool_min_free :%d
  ", obj_pool_min_free);
  	seq_printf(m, "pool_used     :%d
  ", obj_pool_used);
  	seq_printf(m, "pool_max_used :%d
  ", obj_pool_max_used);
  	return 0;
  }
  
  static int debug_stats_open(struct inode *inode, struct file *filp)
  {
  	return single_open(filp, debug_stats_show, NULL);
  }
  
  static const struct file_operations debug_stats_fops = {
  	.open		= debug_stats_open,
  	.read		= seq_read,
  	.llseek		= seq_lseek,
  	.release	= single_release,
  };
  
  static int __init debug_objects_init_debugfs(void)
  {
  	struct dentry *dbgdir, *dbgstats;
  
  	if (!debug_objects_enabled)
  		return 0;
  
  	dbgdir = debugfs_create_dir("debug_objects", NULL);
  	if (!dbgdir)
  		return -ENOMEM;
  
  	dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
  				       &debug_stats_fops);
  	if (!dbgstats)
  		goto err;
  
  	return 0;
  
  err:
  	debugfs_remove(dbgdir);
  
  	return -ENOMEM;
  }
  __initcall(debug_objects_init_debugfs);
  
  #else
  static inline void debug_objects_init_debugfs(void) { }
  #endif
  
  #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
  
  /* Random data structure for the self test */
  struct self_test {
  	unsigned long	dummy1[6];
  	int		static_init;
  	unsigned long	dummy2[3];
  };
  
  static __initdata struct debug_obj_descr descr_type_test;
  
  /*
   * fixup_init is called when:
   * - an active object is initialized
   */
  static int __init fixup_init(void *addr, enum debug_obj_state state)
  {
  	struct self_test *obj = addr;
  
  	switch (state) {
  	case ODEBUG_STATE_ACTIVE:
  		debug_object_deactivate(obj, &descr_type_test);
  		debug_object_init(obj, &descr_type_test);
  		return 1;
  	default:
  		return 0;
  	}
  }
  
  /*
   * fixup_activate is called when:
   * - an active object is activated
   * - an unknown object is activated (might be a statically initialized object)
   */
  static int __init fixup_activate(void *addr, enum debug_obj_state state)
  {
  	struct self_test *obj = addr;
  
  	switch (state) {
  	case ODEBUG_STATE_NOTAVAILABLE:
  		if (obj->static_init == 1) {
  			debug_object_init(obj, &descr_type_test);
  			debug_object_activate(obj, &descr_type_test);
  			/*
  			 * Real code should return 0 here ! This is
  			 * not a fixup of some bad behaviour. We
  			 * merily call the debug_init function to keep
  			 * track of the object.
  			 */
  			return 1;
  		} else {
  			/* Real code needs to emit a warning here */
  		}
  		return 0;
  
  	case ODEBUG_STATE_ACTIVE:
  		debug_object_deactivate(obj, &descr_type_test);
  		debug_object_activate(obj, &descr_type_test);
  		return 1;
  
  	default:
  		return 0;
  	}
  }
  
  /*
   * fixup_destroy is called when:
   * - an active object is destroyed
   */
  static int __init fixup_destroy(void *addr, enum debug_obj_state state)
  {
  	struct self_test *obj = addr;
  
  	switch (state) {
  	case ODEBUG_STATE_ACTIVE:
  		debug_object_deactivate(obj, &descr_type_test);
  		debug_object_destroy(obj, &descr_type_test);
  		return 1;
  	default:
  		return 0;
  	}
  }
  
  /*
   * fixup_free is called when:
   * - an active object is freed
   */
  static int __init fixup_free(void *addr, enum debug_obj_state state)
  {
  	struct self_test *obj = addr;
  
  	switch (state) {
  	case ODEBUG_STATE_ACTIVE:
  		debug_object_deactivate(obj, &descr_type_test);
  		debug_object_free(obj, &descr_type_test);
  		return 1;
  	default:
  		return 0;
  	}
  }
  
  static int
  check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
  {
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
  	int res = -EINVAL;
  
  	db = get_bucket((unsigned long) addr);
  
  	spin_lock_irqsave(&db->lock, flags);
  
  	obj = lookup_object(addr, db);
  	if (!obj && state != ODEBUG_STATE_NONE) {
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
755
756
  		WARN(1, KERN_ERR "ODEBUG: selftest object not found
  ");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
757
758
759
  		goto out;
  	}
  	if (obj && obj->state != state) {
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
760
761
  		WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d
  ",
3ac7fe5a4   Thomas Gleixner   infrastructure to...
762
  		       obj->state, state);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
763
764
765
  		goto out;
  	}
  	if (fixups != debug_objects_fixups) {
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
766
767
  		WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d
  ",
3ac7fe5a4   Thomas Gleixner   infrastructure to...
768
  		       fixups, debug_objects_fixups);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
769
770
771
  		goto out;
  	}
  	if (warnings != debug_objects_warnings) {
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
772
773
  		WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d
  ",
3ac7fe5a4   Thomas Gleixner   infrastructure to...
774
  		       warnings, debug_objects_warnings);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
  		goto out;
  	}
  	res = 0;
  out:
  	spin_unlock_irqrestore(&db->lock, flags);
  	if (res)
  		debug_objects_enabled = 0;
  	return res;
  }
  
  static __initdata struct debug_obj_descr descr_type_test = {
  	.name			= "selftest",
  	.fixup_init		= fixup_init,
  	.fixup_activate		= fixup_activate,
  	.fixup_destroy		= fixup_destroy,
  	.fixup_free		= fixup_free,
  };
  
  static __initdata struct self_test obj = { .static_init = 0 };
  
  static void __init debug_objects_selftest(void)
  {
  	int fixups, oldfixups, warnings, oldwarnings;
  	unsigned long flags;
  
  	local_irq_save(flags);
  
  	fixups = oldfixups = debug_objects_fixups;
  	warnings = oldwarnings = debug_objects_warnings;
  	descr_test = &descr_type_test;
  
  	debug_object_init(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
  		goto out;
  	debug_object_activate(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
  		goto out;
  	debug_object_activate(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
  		goto out;
  	debug_object_deactivate(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
  		goto out;
  	debug_object_destroy(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
  		goto out;
  	debug_object_init(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
  		goto out;
  	debug_object_activate(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
  		goto out;
  	debug_object_deactivate(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
  		goto out;
  	debug_object_free(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
  		goto out;
  
  	obj.static_init = 1;
  	debug_object_activate(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, warnings))
  		goto out;
  	debug_object_init(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
  		goto out;
  	debug_object_free(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
  		goto out;
  
  #ifdef CONFIG_DEBUG_OBJECTS_FREE
  	debug_object_init(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
  		goto out;
  	debug_object_activate(&obj, &descr_type_test);
  	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
  		goto out;
  	__debug_check_no_obj_freed(&obj, sizeof(obj));
  	if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
  		goto out;
  #endif
  	printk(KERN_INFO "ODEBUG: selftest passed
  ");
  
  out:
  	debug_objects_fixups = oldfixups;
  	debug_objects_warnings = oldwarnings;
  	descr_test = NULL;
  
  	local_irq_restore(flags);
  }
  #else
  static inline void debug_objects_selftest(void) { }
  #endif
  
  /*
   * Called during early boot to initialize the hash buckets and link
   * the static object pool objects into the poll list. After this call
   * the object tracker is fully operational.
   */
  void __init debug_objects_early_init(void)
  {
  	int i;
  
  	for (i = 0; i < ODEBUG_HASH_SIZE; i++)
  		spin_lock_init(&obj_hash[i].lock);
  
  	for (i = 0; i < ODEBUG_POOL_SIZE; i++)
  		hlist_add_head(&obj_static_pool[i].node, &obj_pool);
  }
  
  /*
   * Called after the kmem_caches are functional to setup a dedicated
   * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
   * prevents that the debug code is called on kmem_cache_free() for the
   * debug tracker objects to avoid recursive calls.
   */
  void __init debug_objects_mem_init(void)
  {
  	if (!debug_objects_enabled)
  		return;
  
  	obj_cache = kmem_cache_create("debug_objects_cache",
  				      sizeof (struct debug_obj), 0,
  				      SLAB_DEBUG_OBJECTS, NULL);
  
  	if (!obj_cache)
  		debug_objects_enabled = 0;
  	else
  		debug_objects_selftest();
  }