Blame view

lib/debugobjects.c 28.3 KB
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1
2
3
4
5
6
7
8
9
  /*
   * 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
   */
719e48439   Fabian Frederick   lib/debugobjects....
10
11
  
  #define pr_fmt(fmt) "ODEBUG: " fmt
3ac7fe5a4   Thomas Gleixner   infrastructure to...
12
13
  #include <linux/debugobjects.h>
  #include <linux/interrupt.h>
d43c36dc6   Alexey Dobriyan   headers: remove s...
14
  #include <linux/sched.h>
68db0cf10   Ingo Molnar   sched/headers: Pr...
15
  #include <linux/sched/task_stack.h>
3ac7fe5a4   Thomas Gleixner   infrastructure to...
16
17
  #include <linux/seq_file.h>
  #include <linux/debugfs.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
18
  #include <linux/slab.h>
3ac7fe5a4   Thomas Gleixner   infrastructure to...
19
  #include <linux/hash.h>
caba4cbbd   Waiman Long   debugobjects: Mak...
20
  #include <linux/kmemleak.h>
3ac7fe5a4   Thomas Gleixner   infrastructure to...
21
22
23
  
  #define ODEBUG_HASH_BITS	14
  #define ODEBUG_HASH_SIZE	(1 << ODEBUG_HASH_BITS)
0b6ec8c0a   Christian Borntraeger   debugobjects: All...
24
  #define ODEBUG_POOL_SIZE	1024
3ac7fe5a4   Thomas Gleixner   infrastructure to...
25
26
27
28
29
30
31
32
  #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;
aef9cb052   Thomas Gleixner   debugobjects: Con...
33
  	raw_spinlock_t		lock;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
34
35
36
  };
  
  static struct debug_bucket	obj_hash[ODEBUG_HASH_SIZE];
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
37
  static struct debug_obj		obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
38

aef9cb052   Thomas Gleixner   debugobjects: Con...
39
  static DEFINE_RAW_SPINLOCK(pool_lock);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
40
41
42
43
44
45
46
47
48
49
50
51
  
  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;
3ae702054   Ingo Molnar   debugobjects: add...
52
53
  static int			debug_objects_enabled __read_mostly
  				= CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
97dd552eb   Waiman Long   debugobjects: Sca...
54
55
56
57
  static int			debug_objects_pool_size __read_mostly
  				= ODEBUG_POOL_SIZE;
  static int			debug_objects_pool_min_level __read_mostly
  				= ODEBUG_POOL_MIN_LEVEL;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
58
  static struct debug_obj_descr	*descr_test  __read_mostly;
c4b73aabd   Waiman Long   debugobjects: Tra...
59
  /*
0cad93c34   Waiman Long   debugobjects: Imp...
60
   * Track numbers of kmem_cache_alloc()/free() calls done.
c4b73aabd   Waiman Long   debugobjects: Tra...
61
   */
0cad93c34   Waiman Long   debugobjects: Imp...
62
  static int			debug_objects_allocated;
c4b73aabd   Waiman Long   debugobjects: Tra...
63
  static int			debug_objects_freed;
337fff8b5   Thomas Gleixner   debugobjects: del...
64
65
  static void free_obj_work(struct work_struct *work);
  static DECLARE_WORK(debug_obj_work, free_obj_work);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
66
67
68
69
70
  static int __init enable_object_debug(char *str)
  {
  	debug_objects_enabled = 1;
  	return 0;
  }
3e8ebb5c4   Kyle McMartin   debug_objects: ad...
71
72
73
74
75
76
  
  static int __init disable_object_debug(char *str)
  {
  	debug_objects_enabled = 0;
  	return 0;
  }
3ac7fe5a4   Thomas Gleixner   infrastructure to...
77
  early_param("debug_objects", enable_object_debug);
3e8ebb5c4   Kyle McMartin   debug_objects: ad...
78
  early_param("no_debug_objects", disable_object_debug);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
79
80
81
82
83
84
85
86
87
  
  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",
  };
1fda107d4   Thomas Gleixner   debugobjects: Rem...
88
  static void fill_pool(void)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
89
90
91
  {
  	gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
  	struct debug_obj *new;
50db04dd9   Vegard Nossum   debugobjects: fix...
92
  	unsigned long flags;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
93

97dd552eb   Waiman Long   debugobjects: Sca...
94
  	if (likely(obj_pool_free >= debug_objects_pool_min_level))
1fda107d4   Thomas Gleixner   debugobjects: Rem...
95
  		return;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
96
97
  
  	if (unlikely(!obj_cache))
1fda107d4   Thomas Gleixner   debugobjects: Rem...
98
  		return;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
99

97dd552eb   Waiman Long   debugobjects: Sca...
100
  	while (obj_pool_free < debug_objects_pool_min_level) {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
101
102
103
  
  		new = kmem_cache_zalloc(obj_cache, gfp);
  		if (!new)
3340808cf   Dan Carpenter   debugobjects: Fil...
104
  			return;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
105

aef9cb052   Thomas Gleixner   debugobjects: Con...
106
  		raw_spin_lock_irqsave(&pool_lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
107
  		hlist_add_head(&new->node, &obj_pool);
0cad93c34   Waiman Long   debugobjects: Imp...
108
  		debug_objects_allocated++;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
109
  		obj_pool_free++;
aef9cb052   Thomas Gleixner   debugobjects: Con...
110
  		raw_spin_unlock_irqrestore(&pool_lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
111
  	}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
112
113
114
115
116
117
118
  }
  
  /*
   * Lookup an object in the hash bucket.
   */
  static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
  {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
119
120
  	struct debug_obj *obj;
  	int cnt = 0;
b67bfe0d4   Sasha Levin   hlist: drop the n...
121
  	hlist_for_each_entry(obj, &b->list, node) {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
122
123
124
125
126
127
128
129
130
131
132
  		cnt++;
  		if (obj->object == addr)
  			return obj;
  	}
  	if (cnt > debug_objects_maxchain)
  		debug_objects_maxchain = cnt;
  
  	return NULL;
  }
  
  /*
50db04dd9   Vegard Nossum   debugobjects: fix...
133
   * Allocate a new object. If the pool is empty, switch off the debugger.
673d62cc5   Vegard Nossum   debugobjects: fix...
134
   * Must be called with interrupts disabled.
3ac7fe5a4   Thomas Gleixner   infrastructure to...
135
136
137
138
139
   */
  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...
140

aef9cb052   Thomas Gleixner   debugobjects: Con...
141
  	raw_spin_lock(&pool_lock);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
142
143
144
145
146
147
  	if (obj_pool.first) {
  		obj	    = hlist_entry(obj_pool.first, typeof(*obj), node);
  
  		obj->object = addr;
  		obj->descr  = descr;
  		obj->state  = ODEBUG_STATE_NONE;
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
148
  		obj->astate = 0;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
149
150
151
152
153
154
155
156
157
158
159
160
  		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;
  	}
aef9cb052   Thomas Gleixner   debugobjects: Con...
161
  	raw_spin_unlock(&pool_lock);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
162

3ac7fe5a4   Thomas Gleixner   infrastructure to...
163
164
165
166
  	return obj;
  }
  
  /*
337fff8b5   Thomas Gleixner   debugobjects: del...
167
   * workqueue function to free objects.
858274b6a   Waiman Long   debugobjects: Red...
168
169
170
171
   *
   * To reduce contention on the global pool_lock, the actual freeing of
   * debug objects will be delayed if the pool_lock is busy. We also free
   * the objects in a batch of 4 for each lock/unlock cycle.
3ac7fe5a4   Thomas Gleixner   infrastructure to...
172
   */
858274b6a   Waiman Long   debugobjects: Red...
173
  #define ODEBUG_FREE_BATCH	4
337fff8b5   Thomas Gleixner   debugobjects: del...
174
  static void free_obj_work(struct work_struct *work)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
175
  {
858274b6a   Waiman Long   debugobjects: Red...
176
  	struct debug_obj *objs[ODEBUG_FREE_BATCH];
673d62cc5   Vegard Nossum   debugobjects: fix...
177
  	unsigned long flags;
858274b6a   Waiman Long   debugobjects: Red...
178
  	int i;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
179

858274b6a   Waiman Long   debugobjects: Red...
180
181
182
183
184
185
186
187
188
189
190
  	if (!raw_spin_trylock_irqsave(&pool_lock, flags))
  		return;
  	while (obj_pool_free >= debug_objects_pool_size + ODEBUG_FREE_BATCH) {
  		for (i = 0; i < ODEBUG_FREE_BATCH; i++) {
  			objs[i] = hlist_entry(obj_pool.first,
  					      typeof(*objs[0]), node);
  			hlist_del(&objs[i]->node);
  		}
  
  		obj_pool_free -= ODEBUG_FREE_BATCH;
  		debug_objects_freed += ODEBUG_FREE_BATCH;
337fff8b5   Thomas Gleixner   debugobjects: del...
191
192
193
194
  		/*
  		 * We release pool_lock across kmem_cache_free() to
  		 * avoid contention on pool_lock.
  		 */
aef9cb052   Thomas Gleixner   debugobjects: Con...
195
  		raw_spin_unlock_irqrestore(&pool_lock, flags);
858274b6a   Waiman Long   debugobjects: Red...
196
197
198
199
  		for (i = 0; i < ODEBUG_FREE_BATCH; i++)
  			kmem_cache_free(obj_cache, objs[i]);
  		if (!raw_spin_trylock_irqsave(&pool_lock, flags))
  			return;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
200
  	}
aef9cb052   Thomas Gleixner   debugobjects: Con...
201
  	raw_spin_unlock_irqrestore(&pool_lock, flags);
337fff8b5   Thomas Gleixner   debugobjects: del...
202
203
204
205
206
207
208
209
210
211
  }
  
  /*
   * Put the object back into the pool and schedule work to free objects
   * if necessary.
   */
  static void free_object(struct debug_obj *obj)
  {
  	unsigned long flags;
  	int sched = 0;
aef9cb052   Thomas Gleixner   debugobjects: Con...
212
  	raw_spin_lock_irqsave(&pool_lock, flags);
337fff8b5   Thomas Gleixner   debugobjects: del...
213
214
215
216
  	/*
  	 * schedule work when the pool is filled and the cache is
  	 * initialized:
  	 */
97dd552eb   Waiman Long   debugobjects: Sca...
217
  	if (obj_pool_free > debug_objects_pool_size && obj_cache)
7092dff2a   Tejun Heo   debugobj, workque...
218
  		sched = 1;
337fff8b5   Thomas Gleixner   debugobjects: del...
219
220
221
  	hlist_add_head(&obj->node, &obj_pool);
  	obj_pool_free++;
  	obj_pool_used--;
aef9cb052   Thomas Gleixner   debugobjects: Con...
222
  	raw_spin_unlock_irqrestore(&pool_lock, flags);
337fff8b5   Thomas Gleixner   debugobjects: del...
223
224
  	if (sched)
  		schedule_work(&debug_obj_work);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
225
226
227
228
229
230
231
232
233
  }
  
  /*
   * 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;
b67bfe0d4   Sasha Levin   hlist: drop the n...
234
  	struct hlist_node *tmp;
673d62cc5   Vegard Nossum   debugobjects: fix...
235
  	HLIST_HEAD(freelist);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
236
237
238
  	struct debug_obj *obj;
  	unsigned long flags;
  	int i;
719e48439   Fabian Frederick   lib/debugobjects....
239
240
  	pr_warn("Out of memory. ODEBUG disabled
  ");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
241
242
  
  	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
aef9cb052   Thomas Gleixner   debugobjects: Con...
243
  		raw_spin_lock_irqsave(&db->lock, flags);
673d62cc5   Vegard Nossum   debugobjects: fix...
244
  		hlist_move_list(&db->list, &freelist);
aef9cb052   Thomas Gleixner   debugobjects: Con...
245
  		raw_spin_unlock_irqrestore(&db->lock, flags);
673d62cc5   Vegard Nossum   debugobjects: fix...
246
247
  
  		/* Now free them */
b67bfe0d4   Sasha Levin   hlist: drop the n...
248
  		hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
249
250
251
  			hlist_del(&obj->node);
  			free_object(obj);
  		}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
  	}
  }
  
  /*
   * 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)
  {
997772884   Stanislaw Gruszka   debugobjects: Add...
269
  	struct debug_obj_descr *descr = obj->descr;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
270
  	static int limit;
997772884   Stanislaw Gruszka   debugobjects: Add...
271
272
273
  	if (limit < 5 && descr != descr_test) {
  		void *hint = descr->debug_hint ?
  			descr->debug_hint(obj->object) : NULL;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
274
  		limit++;
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
275
  		WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
997772884   Stanislaw Gruszka   debugobjects: Add...
276
277
  				 "object type: %s hint: %pS
  ",
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
278
  			msg, obj_states[obj->state], obj->astate,
997772884   Stanislaw Gruszka   debugobjects: Add...
279
  			descr->name, hint);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
280
281
282
283
284
285
286
287
  	}
  	debug_objects_warnings++;
  }
  
  /*
   * Try to repair the damage, so we have a better chance to get useful
   * debug output.
   */
b1e4d9d82   Du, Changbin   debugobjects: mak...
288
289
  static bool
  debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
3ac7fe5a4   Thomas Gleixner   infrastructure to...
290
291
  		   void * addr, enum debug_obj_state state)
  {
b1e4d9d82   Du, Changbin   debugobjects: mak...
292
293
294
295
296
  	if (fixup && fixup(addr, state)) {
  		debug_objects_fixups++;
  		return true;
  	}
  	return false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
297
298
299
300
  }
  
  static void debug_object_is_on_stack(void *addr, int onstack)
  {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
301
302
303
304
305
  	int is_on_stack;
  	static int limit;
  
  	if (limit > 4)
  		return;
8b05c7e6e   FUJITA Tomonori   add a helper func...
306
  	is_on_stack = object_is_on_stack(addr);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
307
308
309
310
311
  	if (is_on_stack == onstack)
  		return;
  
  	limit++;
  	if (is_on_stack)
8d015a362   Joel Fernandes (Google)   debugobjects: Mak...
312
313
314
  		pr_warn("object %p is on stack %p, but NOT annotated.
  ", addr,
  			 task_stack_page(current));
3ac7fe5a4   Thomas Gleixner   infrastructure to...
315
  	else
8d015a362   Joel Fernandes (Google)   debugobjects: Mak...
316
317
318
  		pr_warn("object %p is NOT on stack %p, but annotated.
  ", addr,
  			 task_stack_page(current));
3ac7fe5a4   Thomas Gleixner   infrastructure to...
319
320
321
322
323
324
325
326
327
328
  	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...
329
  	fill_pool();
3ac7fe5a4   Thomas Gleixner   infrastructure to...
330
  	db = get_bucket((unsigned long) addr);
aef9cb052   Thomas Gleixner   debugobjects: Con...
331
  	raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
332
333
334
335
336
337
  
  	obj = lookup_object(addr, db);
  	if (!obj) {
  		obj = alloc_object(addr, db, descr);
  		if (!obj) {
  			debug_objects_enabled = 0;
aef9cb052   Thomas Gleixner   debugobjects: Con...
338
  			raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
  			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;
aef9cb052   Thomas Gleixner   debugobjects: Con...
355
  		raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
356
357
358
359
360
361
362
363
364
  		debug_object_fixup(descr->fixup_init, addr, state);
  		return;
  
  	case ODEBUG_STATE_DESTROYED:
  		debug_print_object(obj, "init");
  		break;
  	default:
  		break;
  	}
aef9cb052   Thomas Gleixner   debugobjects: Con...
365
  	raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
366
367
368
369
370
371
372
373
374
375
376
377
378
379
  }
  
  /**
   * 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);
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
380
  EXPORT_SYMBOL_GPL(debug_object_init);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
381
382
383
384
385
386
387
388
389
390
391
392
393
394
  
  /**
   * 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);
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
395
  EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
396
397
398
399
400
  
  /**
   * debug_object_activate - debug checks when an object is activated
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
b778ae253   Paul E. McKenney   debugobjects: Mak...
401
   * Returns 0 for success, -EINVAL for check failed.
3ac7fe5a4   Thomas Gleixner   infrastructure to...
402
   */
b778ae253   Paul E. McKenney   debugobjects: Mak...
403
  int debug_object_activate(void *addr, struct debug_obj_descr *descr)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
404
405
406
407
408
  {
  	enum debug_obj_state state;
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
b778ae253   Paul E. McKenney   debugobjects: Mak...
409
  	int ret;
feac18dda   Stephen Boyd   debugobjects: Be ...
410
411
412
  	struct debug_obj o = { .object = addr,
  			       .state = ODEBUG_STATE_NOTAVAILABLE,
  			       .descr = descr };
3ac7fe5a4   Thomas Gleixner   infrastructure to...
413
414
  
  	if (!debug_objects_enabled)
b778ae253   Paul E. McKenney   debugobjects: Mak...
415
  		return 0;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
416
417
  
  	db = get_bucket((unsigned long) addr);
aef9cb052   Thomas Gleixner   debugobjects: Con...
418
  	raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
419
420
421
422
423
424
425
  
  	obj = lookup_object(addr, db);
  	if (obj) {
  		switch (obj->state) {
  		case ODEBUG_STATE_INIT:
  		case ODEBUG_STATE_INACTIVE:
  			obj->state = ODEBUG_STATE_ACTIVE;
b778ae253   Paul E. McKenney   debugobjects: Mak...
426
  			ret = 0;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
427
428
429
430
431
  			break;
  
  		case ODEBUG_STATE_ACTIVE:
  			debug_print_object(obj, "activate");
  			state = obj->state;
aef9cb052   Thomas Gleixner   debugobjects: Con...
432
  			raw_spin_unlock_irqrestore(&db->lock, flags);
b778ae253   Paul E. McKenney   debugobjects: Mak...
433
  			ret = debug_object_fixup(descr->fixup_activate, addr, state);
e7a8e78bd   Du, Changbin   debugobjects: cor...
434
  			return ret ? 0 : -EINVAL;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
435
436
437
  
  		case ODEBUG_STATE_DESTROYED:
  			debug_print_object(obj, "activate");
b778ae253   Paul E. McKenney   debugobjects: Mak...
438
  			ret = -EINVAL;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
439
440
  			break;
  		default:
b778ae253   Paul E. McKenney   debugobjects: Mak...
441
  			ret = 0;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
442
443
  			break;
  		}
aef9cb052   Thomas Gleixner   debugobjects: Con...
444
  		raw_spin_unlock_irqrestore(&db->lock, flags);
b778ae253   Paul E. McKenney   debugobjects: Mak...
445
  		return ret;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
446
  	}
aef9cb052   Thomas Gleixner   debugobjects: Con...
447
  	raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
448
  	/*
b9fdac7f6   Du, Changbin   debugobjects: ins...
449
450
451
452
453
  	 * We are here when a static object is activated. We
  	 * let the type specific code confirm whether this is
  	 * true or not. if true, we just make sure that the
  	 * static object is tracked in the object tracker. If
  	 * not, this must be a bug, so we try to fix it up.
3ac7fe5a4   Thomas Gleixner   infrastructure to...
454
  	 */
b9fdac7f6   Du, Changbin   debugobjects: ins...
455
456
457
458
459
  	if (descr->is_static_object && descr->is_static_object(addr)) {
  		/* track this static object */
  		debug_object_init(addr, descr);
  		debug_object_activate(addr, descr);
  	} else {
feac18dda   Stephen Boyd   debugobjects: Be ...
460
  		debug_print_object(&o, "activate");
b9fdac7f6   Du, Changbin   debugobjects: ins...
461
462
463
  		ret = debug_object_fixup(descr->fixup_activate, addr,
  					ODEBUG_STATE_NOTAVAILABLE);
  		return ret ? 0 : -EINVAL;
b778ae253   Paul E. McKenney   debugobjects: Mak...
464
465
  	}
  	return 0;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
466
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
467
  EXPORT_SYMBOL_GPL(debug_object_activate);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
  
  /**
   * 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);
aef9cb052   Thomas Gleixner   debugobjects: Con...
484
  	raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
485
486
487
488
489
490
491
  
  	obj = lookup_object(addr, db);
  	if (obj) {
  		switch (obj->state) {
  		case ODEBUG_STATE_INIT:
  		case ODEBUG_STATE_INACTIVE:
  		case ODEBUG_STATE_ACTIVE:
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
492
493
494
495
  			if (!obj->astate)
  				obj->state = ODEBUG_STATE_INACTIVE;
  			else
  				debug_print_object(obj, "deactivate");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
  			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");
  	}
aef9cb052   Thomas Gleixner   debugobjects: Con...
511
  	raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
512
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
513
  EXPORT_SYMBOL_GPL(debug_object_deactivate);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
  
  /**
   * 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);
aef9cb052   Thomas Gleixner   debugobjects: Con...
531
  	raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
532
533
534
535
536
537
538
539
540
541
542
543
544
545
  
  	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;
aef9cb052   Thomas Gleixner   debugobjects: Con...
546
  		raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
547
548
549
550
551
552
553
554
555
556
  		debug_object_fixup(descr->fixup_destroy, addr, state);
  		return;
  
  	case ODEBUG_STATE_DESTROYED:
  		debug_print_object(obj, "destroy");
  		break;
  	default:
  		break;
  	}
  out_unlock:
aef9cb052   Thomas Gleixner   debugobjects: Con...
557
  	raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
558
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
559
  EXPORT_SYMBOL_GPL(debug_object_destroy);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
  
  /**
   * 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);
aef9cb052   Thomas Gleixner   debugobjects: Con...
577
  	raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
578
579
580
581
582
583
584
585
586
  
  	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;
aef9cb052   Thomas Gleixner   debugobjects: Con...
587
  		raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
588
589
590
591
  		debug_object_fixup(descr->fixup_free, addr, state);
  		return;
  	default:
  		hlist_del(&obj->node);
aef9cb052   Thomas Gleixner   debugobjects: Con...
592
  		raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
593
  		free_object(obj);
673d62cc5   Vegard Nossum   debugobjects: fix...
594
  		return;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
595
596
  	}
  out_unlock:
aef9cb052   Thomas Gleixner   debugobjects: Con...
597
  	raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
598
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
599
  EXPORT_SYMBOL_GPL(debug_object_free);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
600

a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
601
  /**
b84d435cc   Christine Chan   debugobjects: Ext...
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
   * debug_object_assert_init - debug checks when object should be init-ed
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
   */
  void debug_object_assert_init(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);
  
  	raw_spin_lock_irqsave(&db->lock, flags);
  
  	obj = lookup_object(addr, db);
  	if (!obj) {
  		struct debug_obj o = { .object = addr,
  				       .state = ODEBUG_STATE_NOTAVAILABLE,
  				       .descr = descr };
  
  		raw_spin_unlock_irqrestore(&db->lock, flags);
  		/*
b9fdac7f6   Du, Changbin   debugobjects: ins...
627
628
629
  		 * Maybe the object is static, and we let the type specific
  		 * code confirm. Track this static object if true, else invoke
  		 * fixup.
b84d435cc   Christine Chan   debugobjects: Ext...
630
  		 */
b9fdac7f6   Du, Changbin   debugobjects: ins...
631
632
633
634
  		if (descr->is_static_object && descr->is_static_object(addr)) {
  			/* Track this static object */
  			debug_object_init(addr, descr);
  		} else {
b84d435cc   Christine Chan   debugobjects: Ext...
635
  			debug_print_object(&o, "assert_init");
b9fdac7f6   Du, Changbin   debugobjects: ins...
636
637
638
  			debug_object_fixup(descr->fixup_assert_init, addr,
  					   ODEBUG_STATE_NOTAVAILABLE);
  		}
b84d435cc   Christine Chan   debugobjects: Ext...
639
640
641
642
643
  		return;
  	}
  
  	raw_spin_unlock_irqrestore(&db->lock, flags);
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
644
  EXPORT_SYMBOL_GPL(debug_object_assert_init);
b84d435cc   Christine Chan   debugobjects: Ext...
645
646
  
  /**
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
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
   * debug_object_active_state - debug checks object usage state machine
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
   * @expect:	expected state
   * @next:	state to move to if expected state is found
   */
  void
  debug_object_active_state(void *addr, struct debug_obj_descr *descr,
  			  unsigned int expect, unsigned int next)
  {
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
  
  	if (!debug_objects_enabled)
  		return;
  
  	db = get_bucket((unsigned long) addr);
  
  	raw_spin_lock_irqsave(&db->lock, flags);
  
  	obj = lookup_object(addr, db);
  	if (obj) {
  		switch (obj->state) {
  		case ODEBUG_STATE_ACTIVE:
  			if (obj->astate == expect)
  				obj->astate = next;
  			else
  				debug_print_object(obj, "active_state");
  			break;
  
  		default:
  			debug_print_object(obj, "active_state");
  			break;
  		}
  	} else {
  		struct debug_obj o = { .object = addr,
  				       .state = ODEBUG_STATE_NOTAVAILABLE,
  				       .descr = descr };
  
  		debug_print_object(&o, "active_state");
  	}
  
  	raw_spin_unlock_irqrestore(&db->lock, flags);
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
692
  EXPORT_SYMBOL_GPL(debug_object_active_state);
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
693

3ac7fe5a4   Thomas Gleixner   infrastructure to...
694
695
696
697
  #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;
b67bfe0d4   Sasha Levin   hlist: drop the n...
698
  	struct hlist_node *tmp;
673d62cc5   Vegard Nossum   debugobjects: fix...
699
  	HLIST_HEAD(freelist);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
  	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;
aef9cb052   Thomas Gleixner   debugobjects: Con...
717
  		raw_spin_lock_irqsave(&db->lock, flags);
b67bfe0d4   Sasha Levin   hlist: drop the n...
718
  		hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
719
720
721
722
723
724
725
726
727
728
  			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;
aef9cb052   Thomas Gleixner   debugobjects: Con...
729
  				raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
730
731
732
733
734
  				debug_object_fixup(descr->fixup_free,
  						   (void *) oaddr, state);
  				goto repeat;
  			default:
  				hlist_del(&obj->node);
673d62cc5   Vegard Nossum   debugobjects: fix...
735
  				hlist_add_head(&obj->node, &freelist);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
736
737
738
  				break;
  			}
  		}
aef9cb052   Thomas Gleixner   debugobjects: Con...
739
  		raw_spin_unlock_irqrestore(&db->lock, flags);
673d62cc5   Vegard Nossum   debugobjects: fix...
740
741
  
  		/* Now free them */
b67bfe0d4   Sasha Levin   hlist: drop the n...
742
  		hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
673d62cc5   Vegard Nossum   debugobjects: fix...
743
744
745
  			hlist_del(&obj->node);
  			free_object(obj);
  		}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
  		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);
0cad93c34   Waiman Long   debugobjects: Imp...
776
777
778
779
  	seq_printf(m, "objs_allocated:%d
  ", debug_objects_allocated);
  	seq_printf(m, "objs_freed    :%d
  ", debug_objects_freed);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
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
  	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;
b9fdac7f6   Du, Changbin   debugobjects: ins...
834
835
836
837
838
839
  static bool __init is_static_object(void *addr)
  {
  	struct self_test *obj = addr;
  
  	return obj->static_init;
  }
3ac7fe5a4   Thomas Gleixner   infrastructure to...
840
841
842
843
  /*
   * fixup_init is called when:
   * - an active object is initialized
   */
b1e4d9d82   Du, Changbin   debugobjects: mak...
844
  static bool __init fixup_init(void *addr, enum debug_obj_state state)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
845
846
847
848
849
850
851
  {
  	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);
b1e4d9d82   Du, Changbin   debugobjects: mak...
852
  		return true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
853
  	default:
b1e4d9d82   Du, Changbin   debugobjects: mak...
854
  		return false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
855
856
857
858
859
860
  	}
  }
  
  /*
   * fixup_activate is called when:
   * - an active object is activated
b9fdac7f6   Du, Changbin   debugobjects: ins...
861
   * - an unknown non-static object is activated
3ac7fe5a4   Thomas Gleixner   infrastructure to...
862
   */
b1e4d9d82   Du, Changbin   debugobjects: mak...
863
  static bool __init fixup_activate(void *addr, enum debug_obj_state state)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
864
865
866
867
868
  {
  	struct self_test *obj = addr;
  
  	switch (state) {
  	case ODEBUG_STATE_NOTAVAILABLE:
b1e4d9d82   Du, Changbin   debugobjects: mak...
869
  		return true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
870
871
872
  	case ODEBUG_STATE_ACTIVE:
  		debug_object_deactivate(obj, &descr_type_test);
  		debug_object_activate(obj, &descr_type_test);
b1e4d9d82   Du, Changbin   debugobjects: mak...
873
  		return true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
874
875
  
  	default:
b1e4d9d82   Du, Changbin   debugobjects: mak...
876
  		return false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
877
878
879
880
881
882
883
  	}
  }
  
  /*
   * fixup_destroy is called when:
   * - an active object is destroyed
   */
b1e4d9d82   Du, Changbin   debugobjects: mak...
884
  static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
885
886
887
888
889
890
891
  {
  	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);
b1e4d9d82   Du, Changbin   debugobjects: mak...
892
  		return true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
893
  	default:
b1e4d9d82   Du, Changbin   debugobjects: mak...
894
  		return false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
895
896
897
898
899
900
901
  	}
  }
  
  /*
   * fixup_free is called when:
   * - an active object is freed
   */
b1e4d9d82   Du, Changbin   debugobjects: mak...
902
  static bool __init fixup_free(void *addr, enum debug_obj_state state)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
903
904
905
906
907
908
909
  {
  	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);
b1e4d9d82   Du, Changbin   debugobjects: mak...
910
  		return true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
911
  	default:
b1e4d9d82   Du, Changbin   debugobjects: mak...
912
  		return false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
913
914
  	}
  }
1fb2f77c0   Henrik Kretzschmar   debugobjects: Sec...
915
  static int __init
3ac7fe5a4   Thomas Gleixner   infrastructure to...
916
917
918
919
920
921
922
923
  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);
aef9cb052   Thomas Gleixner   debugobjects: Con...
924
  	raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
925
926
927
  
  	obj = lookup_object(addr, db);
  	if (!obj && state != ODEBUG_STATE_NONE) {
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
928
929
  		WARN(1, KERN_ERR "ODEBUG: selftest object not found
  ");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
930
931
932
  		goto out;
  	}
  	if (obj && obj->state != state) {
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
933
934
  		WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d
  ",
3ac7fe5a4   Thomas Gleixner   infrastructure to...
935
  		       obj->state, state);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
936
937
938
  		goto out;
  	}
  	if (fixups != debug_objects_fixups) {
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
939
940
  		WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d
  ",
3ac7fe5a4   Thomas Gleixner   infrastructure to...
941
  		       fixups, debug_objects_fixups);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
942
943
944
  		goto out;
  	}
  	if (warnings != debug_objects_warnings) {
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
945
946
  		WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d
  ",
3ac7fe5a4   Thomas Gleixner   infrastructure to...
947
  		       warnings, debug_objects_warnings);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
948
949
950
951
  		goto out;
  	}
  	res = 0;
  out:
aef9cb052   Thomas Gleixner   debugobjects: Con...
952
  	raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
953
954
955
956
957
958
959
  	if (res)
  		debug_objects_enabled = 0;
  	return res;
  }
  
  static __initdata struct debug_obj_descr descr_type_test = {
  	.name			= "selftest",
b9fdac7f6   Du, Changbin   debugobjects: ins...
960
  	.is_static_object	= is_static_object,
3ac7fe5a4   Thomas Gleixner   infrastructure to...
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
  	.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);
9f78ff005   Stephen Boyd   debugobjects: Fix...
1010
  	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
  		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
719e48439   Fabian Frederick   lib/debugobjects....
1030
1031
  	pr_info("selftest passed
  ");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
  
  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++)
aef9cb052   Thomas Gleixner   debugobjects: Con...
1054
  		raw_spin_lock_init(&obj_hash[i].lock);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1055
1056
1057
1058
1059
1060
  
  	for (i = 0; i < ODEBUG_POOL_SIZE; i++)
  		hlist_add_head(&obj_static_pool[i].node, &obj_pool);
  }
  
  /*
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1061
1062
   * Convert the statically allocated objects to dynamic ones:
   */
1fb2f77c0   Henrik Kretzschmar   debugobjects: Sec...
1063
  static int __init debug_objects_replace_static_objects(void)
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1064
1065
  {
  	struct debug_bucket *db = obj_hash;
b67bfe0d4   Sasha Levin   hlist: drop the n...
1066
  	struct hlist_node *tmp;
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
  	struct debug_obj *obj, *new;
  	HLIST_HEAD(objects);
  	int i, cnt = 0;
  
  	for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
  		obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
  		if (!obj)
  			goto free;
  		hlist_add_head(&obj->node, &objects);
  	}
  
  	/*
  	 * When debug_objects_mem_init() is called we know that only
  	 * one CPU is up, so disabling interrupts is enough
  	 * protection. This avoids the lockdep hell of lock ordering.
  	 */
  	local_irq_disable();
  
  	/* Remove the statically allocated objects from the pool */
b67bfe0d4   Sasha Levin   hlist: drop the n...
1086
  	hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1087
1088
1089
1090
1091
1092
1093
  		hlist_del(&obj->node);
  	/* Move the allocated objects to the pool */
  	hlist_move_list(&objects, &obj_pool);
  
  	/* Replace the active object references */
  	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
  		hlist_move_list(&db->list, &objects);
b67bfe0d4   Sasha Levin   hlist: drop the n...
1094
  		hlist_for_each_entry(obj, &objects, node) {
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1095
1096
1097
1098
1099
1100
1101
1102
  			new = hlist_entry(obj_pool.first, typeof(*obj), node);
  			hlist_del(&new->node);
  			/* copy object data */
  			*new = *obj;
  			hlist_add_head(&new->node, &db->list);
  			cnt++;
  		}
  	}
765a5e0cb   Thomas Gleixner   debugobjects: pri...
1103
  	local_irq_enable();
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1104

c0f35cc0b   Fabian Frederick   lib/debugobjects....
1105
1106
1107
  	pr_debug("%d of %d active objects replaced
  ",
  		 cnt, obj_pool_used);
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1108
1109
  	return 0;
  free:
b67bfe0d4   Sasha Levin   hlist: drop the n...
1110
  	hlist_for_each_entry_safe(obj, tmp, &objects, node) {
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1111
1112
1113
1114
1115
1116
1117
  		hlist_del(&obj->node);
  		kmem_cache_free(obj_cache, obj);
  	}
  	return -ENOMEM;
  }
  
  /*
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
   * 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,
225b11374   Qian Cai   debugobjects: avo...
1130
1131
  				      SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
  				      NULL);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1132

1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1133
  	if (!obj_cache || debug_objects_replace_static_objects()) {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1134
  		debug_objects_enabled = 0;
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1135
1136
  		if (obj_cache)
  			kmem_cache_destroy(obj_cache);
719e48439   Fabian Frederick   lib/debugobjects....
1137
1138
  		pr_warn("out of memory.
  ");
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1139
  	} else
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1140
  		debug_objects_selftest();
97dd552eb   Waiman Long   debugobjects: Sca...
1141
1142
1143
1144
1145
1146
1147
  
  	/*
  	 * Increase the thresholds for allocating and freeing objects
  	 * according to the number of possible CPUs available in the system.
  	 */
  	debug_objects_pool_size += num_possible_cpus() * 32;
  	debug_objects_pool_min_level += num_possible_cpus() * 4;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1148
  }