Blame view

lib/debugobjects.c 35.4 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>
88451f2cd   Zqiang   debugobjects: Fre...
21
  #include <linux/cpu.h>
3ac7fe5a4   Thomas Gleixner   infrastructure to...
22
23
24
  
  #define ODEBUG_HASH_BITS	14
  #define ODEBUG_HASH_SIZE	(1 << ODEBUG_HASH_BITS)
0b6ec8c0a   Christian Borntraeger   debugobjects: All...
25
  #define ODEBUG_POOL_SIZE	1024
3ac7fe5a4   Thomas Gleixner   infrastructure to...
26
  #define ODEBUG_POOL_MIN_LEVEL	256
d86998b17   Waiman Long   debugobjects: Add...
27
  #define ODEBUG_POOL_PERCPU_SIZE	64
634d61f45   Waiman Long   debugobjects: Per...
28
  #define ODEBUG_BATCH_SIZE	16
3ac7fe5a4   Thomas Gleixner   infrastructure to...
29
30
31
32
  
  #define ODEBUG_CHUNK_SHIFT	PAGE_SHIFT
  #define ODEBUG_CHUNK_SIZE	(1 << ODEBUG_CHUNK_SHIFT)
  #define ODEBUG_CHUNK_MASK	(~(ODEBUG_CHUNK_SIZE - 1))
a7344a68a   Waiman Long   debugobjects: Les...
33
34
35
36
37
38
39
  /*
   * We limit the freeing of debug objects via workqueue at a maximum
   * frequency of 10Hz and about 1024 objects for each freeing operation.
   * So it is freeing at most 10k debug objects per second.
   */
  #define ODEBUG_FREE_WORK_MAX	1024
  #define ODEBUG_FREE_WORK_DELAY	DIV_ROUND_UP(HZ, 10)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
40
41
  struct debug_bucket {
  	struct hlist_head	list;
aef9cb052   Thomas Gleixner   debugobjects: Con...
42
  	raw_spinlock_t		lock;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
43
  };
d86998b17   Waiman Long   debugobjects: Add...
44
45
46
47
48
49
50
51
52
53
  /*
   * Debug object percpu free list
   * Access is protected by disabling irq
   */
  struct debug_percpu_free {
  	struct hlist_head	free_objs;
  	int			obj_free;
  };
  
  static DEFINE_PER_CPU(struct debug_percpu_free, percpu_obj_pool);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
54
  static struct debug_bucket	obj_hash[ODEBUG_HASH_SIZE];
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
55
  static struct debug_obj		obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
56

aef9cb052   Thomas Gleixner   debugobjects: Con...
57
  static DEFINE_RAW_SPINLOCK(pool_lock);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
58
59
  
  static HLIST_HEAD(obj_pool);
36c4ead6f   Yang Shi   debugobjects: Add...
60
  static HLIST_HEAD(obj_to_free);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
61

d86998b17   Waiman Long   debugobjects: Add...
62
63
64
65
66
67
68
  /*
   * Because of the presence of percpu free pools, obj_pool_free will
   * under-count those in the percpu free pools. Similarly, obj_pool_used
   * will over-count those in the percpu free pools. Adjustments will be
   * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
   * can be off.
   */
3ac7fe5a4   Thomas Gleixner   infrastructure to...
69
70
71
72
  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;
a7344a68a   Waiman Long   debugobjects: Les...
73
  static bool			obj_freeing;
36c4ead6f   Yang Shi   debugobjects: Add...
74
75
  /* The number of objs on the global free list */
  static int			obj_nr_tofree;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
76
77
  
  static int			debug_objects_maxchain __read_mostly;
163cf842f   Arnd Bergmann   debugobjects: Avo...
78
  static int __maybe_unused	debug_objects_maxchecked __read_mostly;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
79
80
  static int			debug_objects_fixups __read_mostly;
  static int			debug_objects_warnings __read_mostly;
3ae702054   Ingo Molnar   debugobjects: add...
81
82
  static int			debug_objects_enabled __read_mostly
  				= CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
97dd552eb   Waiman Long   debugobjects: Sca...
83
84
85
86
  static int			debug_objects_pool_size __read_mostly
  				= ODEBUG_POOL_SIZE;
  static int			debug_objects_pool_min_level __read_mostly
  				= ODEBUG_POOL_MIN_LEVEL;
aedcade6f   Stephen Boyd   debugobjects: All...
87
  static const struct debug_obj_descr *descr_test  __read_mostly;
d86998b17   Waiman Long   debugobjects: Add...
88
  static struct kmem_cache	*obj_cache __read_mostly;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
89

c4b73aabd   Waiman Long   debugobjects: Tra...
90
  /*
0cad93c34   Waiman Long   debugobjects: Imp...
91
   * Track numbers of kmem_cache_alloc()/free() calls done.
c4b73aabd   Waiman Long   debugobjects: Tra...
92
   */
0cad93c34   Waiman Long   debugobjects: Imp...
93
  static int			debug_objects_allocated;
c4b73aabd   Waiman Long   debugobjects: Tra...
94
  static int			debug_objects_freed;
337fff8b5   Thomas Gleixner   debugobjects: del...
95
  static void free_obj_work(struct work_struct *work);
a7344a68a   Waiman Long   debugobjects: Les...
96
  static DECLARE_DELAYED_WORK(debug_obj_work, free_obj_work);
337fff8b5   Thomas Gleixner   debugobjects: del...
97

3ac7fe5a4   Thomas Gleixner   infrastructure to...
98
99
100
101
102
  static int __init enable_object_debug(char *str)
  {
  	debug_objects_enabled = 1;
  	return 0;
  }
3e8ebb5c4   Kyle McMartin   debug_objects: ad...
103
104
105
106
107
108
  
  static int __init disable_object_debug(char *str)
  {
  	debug_objects_enabled = 0;
  	return 0;
  }
3ac7fe5a4   Thomas Gleixner   infrastructure to...
109
  early_param("debug_objects", enable_object_debug);
3e8ebb5c4   Kyle McMartin   debug_objects: ad...
110
  early_param("no_debug_objects", disable_object_debug);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
111
112
113
114
115
116
117
118
119
  
  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...
120
  static void fill_pool(void)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
121
122
  {
  	gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
d26bf5056   Waiman Long   debugobjects: Red...
123
  	struct debug_obj *obj;
50db04dd9   Vegard Nossum   debugobjects: fix...
124
  	unsigned long flags;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
125

35fd7a637   Marco Elver   debugobjects: Fix...
126
  	if (likely(READ_ONCE(obj_pool_free) >= debug_objects_pool_min_level))
1fda107d4   Thomas Gleixner   debugobjects: Rem...
127
  		return;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
128

36c4ead6f   Yang Shi   debugobjects: Add...
129
130
131
  	/*
  	 * Reuse objs from the global free list; they will be reinitialized
  	 * when allocating.
35fd7a637   Marco Elver   debugobjects: Fix...
132
133
134
135
  	 *
  	 * Both obj_nr_tofree and obj_pool_free are checked locklessly; the
  	 * READ_ONCE()s pair with the WRITE_ONCE()s in pool_lock critical
  	 * sections.
36c4ead6f   Yang Shi   debugobjects: Add...
136
  	 */
35fd7a637   Marco Elver   debugobjects: Fix...
137
  	while (READ_ONCE(obj_nr_tofree) && (READ_ONCE(obj_pool_free) < obj_pool_min_free)) {
36c4ead6f   Yang Shi   debugobjects: Add...
138
139
140
141
142
  		raw_spin_lock_irqsave(&pool_lock, flags);
  		/*
  		 * Recheck with the lock held as the worker thread might have
  		 * won the race and freed the global free list already.
  		 */
d26bf5056   Waiman Long   debugobjects: Red...
143
  		while (obj_nr_tofree && (obj_pool_free < obj_pool_min_free)) {
36c4ead6f   Yang Shi   debugobjects: Add...
144
145
  			obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
  			hlist_del(&obj->node);
35fd7a637   Marco Elver   debugobjects: Fix...
146
  			WRITE_ONCE(obj_nr_tofree, obj_nr_tofree - 1);
36c4ead6f   Yang Shi   debugobjects: Add...
147
  			hlist_add_head(&obj->node, &obj_pool);
35fd7a637   Marco Elver   debugobjects: Fix...
148
  			WRITE_ONCE(obj_pool_free, obj_pool_free + 1);
36c4ead6f   Yang Shi   debugobjects: Add...
149
150
151
  		}
  		raw_spin_unlock_irqrestore(&pool_lock, flags);
  	}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
152
  	if (unlikely(!obj_cache))
1fda107d4   Thomas Gleixner   debugobjects: Rem...
153
  		return;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
154

35fd7a637   Marco Elver   debugobjects: Fix...
155
  	while (READ_ONCE(obj_pool_free) < debug_objects_pool_min_level) {
d26bf5056   Waiman Long   debugobjects: Red...
156
157
  		struct debug_obj *new[ODEBUG_BATCH_SIZE];
  		int cnt;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
158

d26bf5056   Waiman Long   debugobjects: Red...
159
160
161
162
163
164
  		for (cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) {
  			new[cnt] = kmem_cache_zalloc(obj_cache, gfp);
  			if (!new[cnt])
  				break;
  		}
  		if (!cnt)
3340808cf   Dan Carpenter   debugobjects: Fil...
165
  			return;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
166

aef9cb052   Thomas Gleixner   debugobjects: Con...
167
  		raw_spin_lock_irqsave(&pool_lock, flags);
d26bf5056   Waiman Long   debugobjects: Red...
168
169
170
  		while (cnt) {
  			hlist_add_head(&new[--cnt]->node, &obj_pool);
  			debug_objects_allocated++;
35fd7a637   Marco Elver   debugobjects: Fix...
171
  			WRITE_ONCE(obj_pool_free, obj_pool_free + 1);
d26bf5056   Waiman Long   debugobjects: Red...
172
  		}
aef9cb052   Thomas Gleixner   debugobjects: Con...
173
  		raw_spin_unlock_irqrestore(&pool_lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
174
  	}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
175
176
177
178
179
180
181
  }
  
  /*
   * Lookup an object in the hash bucket.
   */
  static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
  {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
182
183
  	struct debug_obj *obj;
  	int cnt = 0;
b67bfe0d4   Sasha Levin   hlist: drop the n...
184
  	hlist_for_each_entry(obj, &b->list, node) {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
185
186
187
188
189
190
191
192
193
194
195
  		cnt++;
  		if (obj->object == addr)
  			return obj;
  	}
  	if (cnt > debug_objects_maxchain)
  		debug_objects_maxchain = cnt;
  
  	return NULL;
  }
  
  /*
d86998b17   Waiman Long   debugobjects: Add...
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
   * Allocate a new object from the hlist
   */
  static struct debug_obj *__alloc_object(struct hlist_head *list)
  {
  	struct debug_obj *obj = NULL;
  
  	if (list->first) {
  		obj = hlist_entry(list->first, typeof(*obj), node);
  		hlist_del(&obj->node);
  	}
  
  	return obj;
  }
  
  /*
50db04dd9   Vegard Nossum   debugobjects: fix...
211
   * Allocate a new object. If the pool is empty, switch off the debugger.
673d62cc5   Vegard Nossum   debugobjects: fix...
212
   * Must be called with interrupts disabled.
3ac7fe5a4   Thomas Gleixner   infrastructure to...
213
214
   */
  static struct debug_obj *
aedcade6f   Stephen Boyd   debugobjects: All...
215
  alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *descr)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
216
  {
634d61f45   Waiman Long   debugobjects: Per...
217
  	struct debug_percpu_free *percpu_pool = this_cpu_ptr(&percpu_obj_pool);
d86998b17   Waiman Long   debugobjects: Add...
218
  	struct debug_obj *obj;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
219

d86998b17   Waiman Long   debugobjects: Add...
220
  	if (likely(obj_cache)) {
d86998b17   Waiman Long   debugobjects: Add...
221
222
223
224
225
226
  		obj = __alloc_object(&percpu_pool->free_objs);
  		if (obj) {
  			percpu_pool->obj_free--;
  			goto init_obj;
  		}
  	}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
227

d86998b17   Waiman Long   debugobjects: Add...
228
229
230
  	raw_spin_lock(&pool_lock);
  	obj = __alloc_object(&obj_pool);
  	if (obj) {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
231
  		obj_pool_used++;
35fd7a637   Marco Elver   debugobjects: Fix...
232
  		WRITE_ONCE(obj_pool_free, obj_pool_free - 1);
634d61f45   Waiman Long   debugobjects: Per...
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  
  		/*
  		 * Looking ahead, allocate one batch of debug objects and
  		 * put them into the percpu free pool.
  		 */
  		if (likely(obj_cache)) {
  			int i;
  
  			for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
  				struct debug_obj *obj2;
  
  				obj2 = __alloc_object(&obj_pool);
  				if (!obj2)
  					break;
  				hlist_add_head(&obj2->node,
  					       &percpu_pool->free_objs);
  				percpu_pool->obj_free++;
  				obj_pool_used++;
35fd7a637   Marco Elver   debugobjects: Fix...
251
  				WRITE_ONCE(obj_pool_free, obj_pool_free - 1);
634d61f45   Waiman Long   debugobjects: Per...
252
253
  			}
  		}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
254
255
  		if (obj_pool_used > obj_pool_max_used)
  			obj_pool_max_used = obj_pool_used;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
256
257
258
  		if (obj_pool_free < obj_pool_min_free)
  			obj_pool_min_free = obj_pool_free;
  	}
aef9cb052   Thomas Gleixner   debugobjects: Con...
259
  	raw_spin_unlock(&pool_lock);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
260

d86998b17   Waiman Long   debugobjects: Add...
261
262
263
264
265
266
267
268
  init_obj:
  	if (obj) {
  		obj->object = addr;
  		obj->descr  = descr;
  		obj->state  = ODEBUG_STATE_NONE;
  		obj->astate = 0;
  		hlist_add_head(&obj->node, &b->list);
  	}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
269
270
271
272
  	return obj;
  }
  
  /*
337fff8b5   Thomas Gleixner   debugobjects: del...
273
   * workqueue function to free objects.
858274b6a   Waiman Long   debugobjects: Red...
274
275
   *
   * To reduce contention on the global pool_lock, the actual freeing of
636e1970f   Yang Shi   debugobjects: Use...
276
   * debug objects will be delayed if the pool_lock is busy.
3ac7fe5a4   Thomas Gleixner   infrastructure to...
277
   */
337fff8b5   Thomas Gleixner   debugobjects: del...
278
  static void free_obj_work(struct work_struct *work)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
279
  {
36c4ead6f   Yang Shi   debugobjects: Add...
280
281
  	struct hlist_node *tmp;
  	struct debug_obj *obj;
673d62cc5   Vegard Nossum   debugobjects: fix...
282
  	unsigned long flags;
36c4ead6f   Yang Shi   debugobjects: Add...
283
  	HLIST_HEAD(tofree);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
284

a7344a68a   Waiman Long   debugobjects: Les...
285
  	WRITE_ONCE(obj_freeing, false);
858274b6a   Waiman Long   debugobjects: Red...
286
287
  	if (!raw_spin_trylock_irqsave(&pool_lock, flags))
  		return;
36c4ead6f   Yang Shi   debugobjects: Add...
288

a7344a68a   Waiman Long   debugobjects: Les...
289
290
  	if (obj_pool_free >= debug_objects_pool_size)
  		goto free_objs;
36c4ead6f   Yang Shi   debugobjects: Add...
291
292
293
  	/*
  	 * The objs on the pool list might be allocated before the work is
  	 * run, so recheck if pool list it full or not, if not fill pool
a7344a68a   Waiman Long   debugobjects: Les...
294
295
296
  	 * list from the global free list. As it is likely that a workload
  	 * may be gearing up to use more and more objects, don't free any
  	 * of them until the next round.
36c4ead6f   Yang Shi   debugobjects: Add...
297
298
299
300
301
  	 */
  	while (obj_nr_tofree && obj_pool_free < debug_objects_pool_size) {
  		obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
  		hlist_del(&obj->node);
  		hlist_add_head(&obj->node, &obj_pool);
35fd7a637   Marco Elver   debugobjects: Fix...
302
303
  		WRITE_ONCE(obj_pool_free, obj_pool_free + 1);
  		WRITE_ONCE(obj_nr_tofree, obj_nr_tofree - 1);
36c4ead6f   Yang Shi   debugobjects: Add...
304
  	}
a7344a68a   Waiman Long   debugobjects: Les...
305
306
  	raw_spin_unlock_irqrestore(&pool_lock, flags);
  	return;
36c4ead6f   Yang Shi   debugobjects: Add...
307

a7344a68a   Waiman Long   debugobjects: Les...
308
  free_objs:
36c4ead6f   Yang Shi   debugobjects: Add...
309
310
311
312
313
314
315
  	/*
  	 * Pool list is already full and there are still objs on the free
  	 * list. Move remaining free objs to a temporary list to free the
  	 * memory outside the pool_lock held region.
  	 */
  	if (obj_nr_tofree) {
  		hlist_move_list(&obj_to_free, &tofree);
04148187a   Arnd Bergmann   debugobjects: Fix...
316
  		debug_objects_freed += obj_nr_tofree;
35fd7a637   Marco Elver   debugobjects: Fix...
317
  		WRITE_ONCE(obj_nr_tofree, 0);
36c4ead6f   Yang Shi   debugobjects: Add...
318
  	}
aef9cb052   Thomas Gleixner   debugobjects: Con...
319
  	raw_spin_unlock_irqrestore(&pool_lock, flags);
36c4ead6f   Yang Shi   debugobjects: Add...
320
321
322
323
324
  
  	hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
  		hlist_del(&obj->node);
  		kmem_cache_free(obj_cache, obj);
  	}
337fff8b5   Thomas Gleixner   debugobjects: del...
325
  }
a7344a68a   Waiman Long   debugobjects: Les...
326
  static void __free_object(struct debug_obj *obj)
337fff8b5   Thomas Gleixner   debugobjects: del...
327
  {
634d61f45   Waiman Long   debugobjects: Per...
328
329
330
  	struct debug_obj *objs[ODEBUG_BATCH_SIZE];
  	struct debug_percpu_free *percpu_pool;
  	int lookahead_count = 0;
337fff8b5   Thomas Gleixner   debugobjects: del...
331
  	unsigned long flags;
636e1970f   Yang Shi   debugobjects: Use...
332
  	bool work;
337fff8b5   Thomas Gleixner   debugobjects: del...
333

d86998b17   Waiman Long   debugobjects: Add...
334
  	local_irq_save(flags);
634d61f45   Waiman Long   debugobjects: Per...
335
336
  	if (!obj_cache)
  		goto free_to_obj_pool;
d86998b17   Waiman Long   debugobjects: Add...
337
338
339
340
  	/*
  	 * Try to free it into the percpu pool first.
  	 */
  	percpu_pool = this_cpu_ptr(&percpu_obj_pool);
634d61f45   Waiman Long   debugobjects: Per...
341
  	if (percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) {
d86998b17   Waiman Long   debugobjects: Add...
342
343
344
  		hlist_add_head(&obj->node, &percpu_pool->free_objs);
  		percpu_pool->obj_free++;
  		local_irq_restore(flags);
a7344a68a   Waiman Long   debugobjects: Les...
345
  		return;
d86998b17   Waiman Long   debugobjects: Add...
346
  	}
634d61f45   Waiman Long   debugobjects: Per...
347
348
349
350
351
352
353
354
355
356
357
358
  	/*
  	 * As the percpu pool is full, look ahead and pull out a batch
  	 * of objects from the percpu pool and free them as well.
  	 */
  	for (; lookahead_count < ODEBUG_BATCH_SIZE; lookahead_count++) {
  		objs[lookahead_count] = __alloc_object(&percpu_pool->free_objs);
  		if (!objs[lookahead_count])
  			break;
  		percpu_pool->obj_free--;
  	}
  
  free_to_obj_pool:
d86998b17   Waiman Long   debugobjects: Add...
359
  	raw_spin_lock(&pool_lock);
a7344a68a   Waiman Long   debugobjects: Les...
360
361
  	work = (obj_pool_free > debug_objects_pool_size) && obj_cache &&
  	       (obj_nr_tofree < ODEBUG_FREE_WORK_MAX);
337fff8b5   Thomas Gleixner   debugobjects: del...
362
  	obj_pool_used--;
636e1970f   Yang Shi   debugobjects: Use...
363
364
  
  	if (work) {
35fd7a637   Marco Elver   debugobjects: Fix...
365
  		WRITE_ONCE(obj_nr_tofree, obj_nr_tofree + 1);
636e1970f   Yang Shi   debugobjects: Use...
366
  		hlist_add_head(&obj->node, &obj_to_free);
634d61f45   Waiman Long   debugobjects: Per...
367
  		if (lookahead_count) {
35fd7a637   Marco Elver   debugobjects: Fix...
368
  			WRITE_ONCE(obj_nr_tofree, obj_nr_tofree + lookahead_count);
634d61f45   Waiman Long   debugobjects: Per...
369
370
371
372
373
374
  			obj_pool_used -= lookahead_count;
  			while (lookahead_count) {
  				hlist_add_head(&objs[--lookahead_count]->node,
  					       &obj_to_free);
  			}
  		}
a7344a68a   Waiman Long   debugobjects: Les...
375
376
377
378
379
380
381
382
383
384
385
  
  		if ((obj_pool_free > debug_objects_pool_size) &&
  		    (obj_nr_tofree < ODEBUG_FREE_WORK_MAX)) {
  			int i;
  
  			/*
  			 * Free one more batch of objects from obj_pool.
  			 */
  			for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
  				obj = __alloc_object(&obj_pool);
  				hlist_add_head(&obj->node, &obj_to_free);
35fd7a637   Marco Elver   debugobjects: Fix...
386
387
  				WRITE_ONCE(obj_pool_free, obj_pool_free - 1);
  				WRITE_ONCE(obj_nr_tofree, obj_nr_tofree + 1);
a7344a68a   Waiman Long   debugobjects: Les...
388
389
  			}
  		}
636e1970f   Yang Shi   debugobjects: Use...
390
  	} else {
35fd7a637   Marco Elver   debugobjects: Fix...
391
  		WRITE_ONCE(obj_pool_free, obj_pool_free + 1);
636e1970f   Yang Shi   debugobjects: Use...
392
  		hlist_add_head(&obj->node, &obj_pool);
634d61f45   Waiman Long   debugobjects: Per...
393
  		if (lookahead_count) {
35fd7a637   Marco Elver   debugobjects: Fix...
394
  			WRITE_ONCE(obj_pool_free, obj_pool_free + lookahead_count);
634d61f45   Waiman Long   debugobjects: Per...
395
396
397
398
399
400
  			obj_pool_used -= lookahead_count;
  			while (lookahead_count) {
  				hlist_add_head(&objs[--lookahead_count]->node,
  					       &obj_pool);
  			}
  		}
636e1970f   Yang Shi   debugobjects: Use...
401
  	}
d86998b17   Waiman Long   debugobjects: Add...
402
403
  	raw_spin_unlock(&pool_lock);
  	local_irq_restore(flags);
636e1970f   Yang Shi   debugobjects: Use...
404
405
406
407
408
409
410
411
  }
  
  /*
   * Put the object back into the pool and schedule work to free objects
   * if necessary.
   */
  static void free_object(struct debug_obj *obj)
  {
a7344a68a   Waiman Long   debugobjects: Les...
412
  	__free_object(obj);
35fd7a637   Marco Elver   debugobjects: Fix...
413
  	if (!READ_ONCE(obj_freeing) && READ_ONCE(obj_nr_tofree)) {
a7344a68a   Waiman Long   debugobjects: Les...
414
415
416
  		WRITE_ONCE(obj_freeing, true);
  		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
  	}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
417
  }
88451f2cd   Zqiang   debugobjects: Fre...
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
  #ifdef CONFIG_HOTPLUG_CPU
  static int object_cpu_offline(unsigned int cpu)
  {
  	struct debug_percpu_free *percpu_pool;
  	struct hlist_node *tmp;
  	struct debug_obj *obj;
  
  	/* Remote access is safe as the CPU is dead already */
  	percpu_pool = per_cpu_ptr(&percpu_obj_pool, cpu);
  	hlist_for_each_entry_safe(obj, tmp, &percpu_pool->free_objs, node) {
  		hlist_del(&obj->node);
  		kmem_cache_free(obj_cache, obj);
  	}
  	percpu_pool->obj_free = 0;
  
  	return 0;
  }
  #endif
3ac7fe5a4   Thomas Gleixner   infrastructure to...
436
437
438
439
440
441
442
  /*
   * 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...
443
  	struct hlist_node *tmp;
673d62cc5   Vegard Nossum   debugobjects: fix...
444
  	HLIST_HEAD(freelist);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
445
446
447
  	struct debug_obj *obj;
  	unsigned long flags;
  	int i;
719e48439   Fabian Frederick   lib/debugobjects....
448
449
  	pr_warn("Out of memory. ODEBUG disabled
  ");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
450
451
  
  	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
aef9cb052   Thomas Gleixner   debugobjects: Con...
452
  		raw_spin_lock_irqsave(&db->lock, flags);
673d62cc5   Vegard Nossum   debugobjects: fix...
453
  		hlist_move_list(&db->list, &freelist);
aef9cb052   Thomas Gleixner   debugobjects: Con...
454
  		raw_spin_unlock_irqrestore(&db->lock, flags);
673d62cc5   Vegard Nossum   debugobjects: fix...
455
456
  
  		/* Now free them */
b67bfe0d4   Sasha Levin   hlist: drop the n...
457
  		hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
458
459
460
  			hlist_del(&obj->node);
  			free_object(obj);
  		}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
  	}
  }
  
  /*
   * 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)
  {
aedcade6f   Stephen Boyd   debugobjects: All...
478
  	const struct debug_obj_descr *descr = obj->descr;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
479
  	static int limit;
997772884   Stanislaw Gruszka   debugobjects: Add...
480
481
482
  	if (limit < 5 && descr != descr_test) {
  		void *hint = descr->debug_hint ?
  			descr->debug_hint(obj->object) : NULL;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
483
  		limit++;
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
484
  		WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
997772884   Stanislaw Gruszka   debugobjects: Add...
485
486
  				 "object type: %s hint: %pS
  ",
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
487
  			msg, obj_states[obj->state], obj->astate,
997772884   Stanislaw Gruszka   debugobjects: Add...
488
  			descr->name, hint);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
489
490
491
492
493
494
495
496
  	}
  	debug_objects_warnings++;
  }
  
  /*
   * Try to repair the damage, so we have a better chance to get useful
   * debug output.
   */
b1e4d9d82   Du, Changbin   debugobjects: mak...
497
498
  static bool
  debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
3ac7fe5a4   Thomas Gleixner   infrastructure to...
499
500
  		   void * addr, enum debug_obj_state state)
  {
b1e4d9d82   Du, Changbin   debugobjects: mak...
501
502
503
504
505
  	if (fixup && fixup(addr, state)) {
  		debug_objects_fixups++;
  		return true;
  	}
  	return false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
506
507
508
509
  }
  
  static void debug_object_is_on_stack(void *addr, int onstack)
  {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
510
511
512
513
514
  	int is_on_stack;
  	static int limit;
  
  	if (limit > 4)
  		return;
8b05c7e6e   FUJITA Tomonori   add a helper func...
515
  	is_on_stack = object_is_on_stack(addr);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
516
517
518
519
520
  	if (is_on_stack == onstack)
  		return;
  
  	limit++;
  	if (is_on_stack)
fc91a3c4c   Joel Fernandes (Google)   debugobjects: Mak...
521
522
523
  		pr_warn("object %p is on stack %p, but NOT annotated.
  ", addr,
  			 task_stack_page(current));
3ac7fe5a4   Thomas Gleixner   infrastructure to...
524
  	else
fc91a3c4c   Joel Fernandes (Google)   debugobjects: Mak...
525
526
527
  		pr_warn("object %p is NOT on stack %p, but annotated.
  ", addr,
  			 task_stack_page(current));
3ac7fe5a4   Thomas Gleixner   infrastructure to...
528
529
530
531
  	WARN_ON(1);
  }
  
  static void
aedcade6f   Stephen Boyd   debugobjects: All...
532
  __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
533
534
  {
  	enum debug_obj_state state;
d5f34153e   Waiman Long   debugobjects: Mov...
535
  	bool check_stack = false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
536
537
538
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
4bedcc284   Thomas Gleixner   debugobjects: Mak...
539
540
541
542
543
544
  	/*
  	 * On RT enabled kernels the pool refill must happen in preemptible
  	 * context:
  	 */
  	if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible())
  		fill_pool();
50db04dd9   Vegard Nossum   debugobjects: fix...
545

3ac7fe5a4   Thomas Gleixner   infrastructure to...
546
  	db = get_bucket((unsigned long) addr);
aef9cb052   Thomas Gleixner   debugobjects: Con...
547
  	raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
548
549
550
551
552
553
  
  	obj = lookup_object(addr, db);
  	if (!obj) {
  		obj = alloc_object(addr, db, descr);
  		if (!obj) {
  			debug_objects_enabled = 0;
aef9cb052   Thomas Gleixner   debugobjects: Con...
554
  			raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
555
556
557
  			debug_objects_oom();
  			return;
  		}
d5f34153e   Waiman Long   debugobjects: Mov...
558
  		check_stack = true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
559
560
561
562
563
564
565
566
567
568
  	}
  
  	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:
3ac7fe5a4   Thomas Gleixner   infrastructure to...
569
  		state = obj->state;
aef9cb052   Thomas Gleixner   debugobjects: Con...
570
  		raw_spin_unlock_irqrestore(&db->lock, flags);
d5f34153e   Waiman Long   debugobjects: Mov...
571
  		debug_print_object(obj, "init");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
572
573
574
575
  		debug_object_fixup(descr->fixup_init, addr, state);
  		return;
  
  	case ODEBUG_STATE_DESTROYED:
d5f34153e   Waiman Long   debugobjects: Mov...
576
  		raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
577
  		debug_print_object(obj, "init");
d5f34153e   Waiman Long   debugobjects: Mov...
578
  		return;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
579
580
581
  	default:
  		break;
  	}
aef9cb052   Thomas Gleixner   debugobjects: Con...
582
  	raw_spin_unlock_irqrestore(&db->lock, flags);
d5f34153e   Waiman Long   debugobjects: Mov...
583
584
  	if (check_stack)
  		debug_object_is_on_stack(addr, onstack);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
585
586
587
588
589
590
591
  }
  
  /**
   * debug_object_init - debug checks when an object is initialized
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
   */
aedcade6f   Stephen Boyd   debugobjects: All...
592
  void debug_object_init(void *addr, const struct debug_obj_descr *descr)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
593
594
595
596
597
598
  {
  	if (!debug_objects_enabled)
  		return;
  
  	__debug_object_init(addr, descr, 0);
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
599
  EXPORT_SYMBOL_GPL(debug_object_init);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
600
601
602
603
604
605
606
  
  /**
   * 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
   */
aedcade6f   Stephen Boyd   debugobjects: All...
607
  void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
608
609
610
611
612
613
  {
  	if (!debug_objects_enabled)
  		return;
  
  	__debug_object_init(addr, descr, 1);
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
614
  EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
615
616
617
618
619
  
  /**
   * 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...
620
   * Returns 0 for success, -EINVAL for check failed.
3ac7fe5a4   Thomas Gleixner   infrastructure to...
621
   */
aedcade6f   Stephen Boyd   debugobjects: All...
622
  int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
623
624
625
626
627
  {
  	enum debug_obj_state state;
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
b778ae253   Paul E. McKenney   debugobjects: Mak...
628
  	int ret;
feac18dda   Stephen Boyd   debugobjects: Be ...
629
630
631
  	struct debug_obj o = { .object = addr,
  			       .state = ODEBUG_STATE_NOTAVAILABLE,
  			       .descr = descr };
3ac7fe5a4   Thomas Gleixner   infrastructure to...
632
633
  
  	if (!debug_objects_enabled)
b778ae253   Paul E. McKenney   debugobjects: Mak...
634
  		return 0;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
635
636
  
  	db = get_bucket((unsigned long) addr);
aef9cb052   Thomas Gleixner   debugobjects: Con...
637
  	raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
638
639
640
  
  	obj = lookup_object(addr, db);
  	if (obj) {
d5f34153e   Waiman Long   debugobjects: Mov...
641
  		bool print_object = false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
642
643
644
645
  		switch (obj->state) {
  		case ODEBUG_STATE_INIT:
  		case ODEBUG_STATE_INACTIVE:
  			obj->state = ODEBUG_STATE_ACTIVE;
b778ae253   Paul E. McKenney   debugobjects: Mak...
646
  			ret = 0;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
647
648
649
  			break;
  
  		case ODEBUG_STATE_ACTIVE:
3ac7fe5a4   Thomas Gleixner   infrastructure to...
650
  			state = obj->state;
aef9cb052   Thomas Gleixner   debugobjects: Con...
651
  			raw_spin_unlock_irqrestore(&db->lock, flags);
d5f34153e   Waiman Long   debugobjects: Mov...
652
  			debug_print_object(obj, "activate");
b778ae253   Paul E. McKenney   debugobjects: Mak...
653
  			ret = debug_object_fixup(descr->fixup_activate, addr, state);
e7a8e78bd   Du, Changbin   debugobjects: cor...
654
  			return ret ? 0 : -EINVAL;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
655
656
  
  		case ODEBUG_STATE_DESTROYED:
d5f34153e   Waiman Long   debugobjects: Mov...
657
  			print_object = true;
b778ae253   Paul E. McKenney   debugobjects: Mak...
658
  			ret = -EINVAL;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
659
660
  			break;
  		default:
b778ae253   Paul E. McKenney   debugobjects: Mak...
661
  			ret = 0;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
662
663
  			break;
  		}
aef9cb052   Thomas Gleixner   debugobjects: Con...
664
  		raw_spin_unlock_irqrestore(&db->lock, flags);
d5f34153e   Waiman Long   debugobjects: Mov...
665
666
  		if (print_object)
  			debug_print_object(obj, "activate");
b778ae253   Paul E. McKenney   debugobjects: Mak...
667
  		return ret;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
668
  	}
aef9cb052   Thomas Gleixner   debugobjects: Con...
669
  	raw_spin_unlock_irqrestore(&db->lock, flags);
d5f34153e   Waiman Long   debugobjects: Mov...
670

3ac7fe5a4   Thomas Gleixner   infrastructure to...
671
  	/*
b9fdac7f6   Du, Changbin   debugobjects: ins...
672
673
674
675
676
  	 * 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...
677
  	 */
b9fdac7f6   Du, Changbin   debugobjects: ins...
678
679
680
681
682
  	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 ...
683
  		debug_print_object(&o, "activate");
b9fdac7f6   Du, Changbin   debugobjects: ins...
684
685
686
  		ret = debug_object_fixup(descr->fixup_activate, addr,
  					ODEBUG_STATE_NOTAVAILABLE);
  		return ret ? 0 : -EINVAL;
b778ae253   Paul E. McKenney   debugobjects: Mak...
687
688
  	}
  	return 0;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
689
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
690
  EXPORT_SYMBOL_GPL(debug_object_activate);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
691
692
693
694
695
696
  
  /**
   * debug_object_deactivate - debug checks when an object is deactivated
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
   */
aedcade6f   Stephen Boyd   debugobjects: All...
697
  void debug_object_deactivate(void *addr, const struct debug_obj_descr *descr)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
698
699
700
701
  {
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
d5f34153e   Waiman Long   debugobjects: Mov...
702
  	bool print_object = false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
703
704
705
706
707
  
  	if (!debug_objects_enabled)
  		return;
  
  	db = get_bucket((unsigned long) addr);
aef9cb052   Thomas Gleixner   debugobjects: Con...
708
  	raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
709
710
711
712
713
714
715
  
  	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...
716
717
718
  			if (!obj->astate)
  				obj->state = ODEBUG_STATE_INACTIVE;
  			else
d5f34153e   Waiman Long   debugobjects: Mov...
719
  				print_object = true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
720
721
722
  			break;
  
  		case ODEBUG_STATE_DESTROYED:
d5f34153e   Waiman Long   debugobjects: Mov...
723
  			print_object = true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
724
725
726
727
  			break;
  		default:
  			break;
  		}
d5f34153e   Waiman Long   debugobjects: Mov...
728
729
730
731
  	}
  
  	raw_spin_unlock_irqrestore(&db->lock, flags);
  	if (!obj) {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
732
733
734
735
736
  		struct debug_obj o = { .object = addr,
  				       .state = ODEBUG_STATE_NOTAVAILABLE,
  				       .descr = descr };
  
  		debug_print_object(&o, "deactivate");
d5f34153e   Waiman Long   debugobjects: Mov...
737
738
  	} else if (print_object) {
  		debug_print_object(obj, "deactivate");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
739
  	}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
740
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
741
  EXPORT_SYMBOL_GPL(debug_object_deactivate);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
742
743
744
745
746
747
  
  /**
   * debug_object_destroy - debug checks when an object is destroyed
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
   */
aedcade6f   Stephen Boyd   debugobjects: All...
748
  void debug_object_destroy(void *addr, const struct debug_obj_descr *descr)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
749
750
751
752
753
  {
  	enum debug_obj_state state;
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
d5f34153e   Waiman Long   debugobjects: Mov...
754
  	bool print_object = false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
755
756
757
758
759
  
  	if (!debug_objects_enabled)
  		return;
  
  	db = get_bucket((unsigned long) addr);
aef9cb052   Thomas Gleixner   debugobjects: Con...
760
  	raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
761
762
763
764
765
766
767
768
769
770
771
772
  
  	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:
3ac7fe5a4   Thomas Gleixner   infrastructure to...
773
  		state = obj->state;
aef9cb052   Thomas Gleixner   debugobjects: Con...
774
  		raw_spin_unlock_irqrestore(&db->lock, flags);
d5f34153e   Waiman Long   debugobjects: Mov...
775
  		debug_print_object(obj, "destroy");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
776
777
778
779
  		debug_object_fixup(descr->fixup_destroy, addr, state);
  		return;
  
  	case ODEBUG_STATE_DESTROYED:
d5f34153e   Waiman Long   debugobjects: Mov...
780
  		print_object = true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
781
782
783
784
785
  		break;
  	default:
  		break;
  	}
  out_unlock:
aef9cb052   Thomas Gleixner   debugobjects: Con...
786
  	raw_spin_unlock_irqrestore(&db->lock, flags);
d5f34153e   Waiman Long   debugobjects: Mov...
787
788
  	if (print_object)
  		debug_print_object(obj, "destroy");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
789
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
790
  EXPORT_SYMBOL_GPL(debug_object_destroy);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
791
792
793
794
795
796
  
  /**
   * debug_object_free - debug checks when an object is freed
   * @addr:	address of the object
   * @descr:	pointer to an object specific debug description structure
   */
aedcade6f   Stephen Boyd   debugobjects: All...
797
  void debug_object_free(void *addr, const struct debug_obj_descr *descr)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
798
799
800
801
802
803
804
805
806
807
  {
  	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...
808
  	raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
809
810
811
812
813
814
815
  
  	obj = lookup_object(addr, db);
  	if (!obj)
  		goto out_unlock;
  
  	switch (obj->state) {
  	case ODEBUG_STATE_ACTIVE:
3ac7fe5a4   Thomas Gleixner   infrastructure to...
816
  		state = obj->state;
aef9cb052   Thomas Gleixner   debugobjects: Con...
817
  		raw_spin_unlock_irqrestore(&db->lock, flags);
d5f34153e   Waiman Long   debugobjects: Mov...
818
  		debug_print_object(obj, "free");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
819
820
821
822
  		debug_object_fixup(descr->fixup_free, addr, state);
  		return;
  	default:
  		hlist_del(&obj->node);
aef9cb052   Thomas Gleixner   debugobjects: Con...
823
  		raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
824
  		free_object(obj);
673d62cc5   Vegard Nossum   debugobjects: fix...
825
  		return;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
826
827
  	}
  out_unlock:
aef9cb052   Thomas Gleixner   debugobjects: Con...
828
  	raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
829
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
830
  EXPORT_SYMBOL_GPL(debug_object_free);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
831

a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
832
  /**
b84d435cc   Christine Chan   debugobjects: Ext...
833
834
835
836
   * 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
   */
aedcade6f   Stephen Boyd   debugobjects: All...
837
  void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
b84d435cc   Christine Chan   debugobjects: Ext...
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
  {
  	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...
858
859
860
  		 * 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...
861
  		 */
b9fdac7f6   Du, Changbin   debugobjects: ins...
862
863
864
865
  		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...
866
  			debug_print_object(&o, "assert_init");
b9fdac7f6   Du, Changbin   debugobjects: ins...
867
868
869
  			debug_object_fixup(descr->fixup_assert_init, addr,
  					   ODEBUG_STATE_NOTAVAILABLE);
  		}
b84d435cc   Christine Chan   debugobjects: Ext...
870
871
872
873
874
  		return;
  	}
  
  	raw_spin_unlock_irqrestore(&db->lock, flags);
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
875
  EXPORT_SYMBOL_GPL(debug_object_assert_init);
b84d435cc   Christine Chan   debugobjects: Ext...
876
877
  
  /**
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
878
879
880
881
882
883
884
   * 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
aedcade6f   Stephen Boyd   debugobjects: All...
885
  debug_object_active_state(void *addr, const struct debug_obj_descr *descr,
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
886
887
888
889
890
  			  unsigned int expect, unsigned int next)
  {
  	struct debug_bucket *db;
  	struct debug_obj *obj;
  	unsigned long flags;
d5f34153e   Waiman Long   debugobjects: Mov...
891
  	bool print_object = false;
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
  
  	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
d5f34153e   Waiman Long   debugobjects: Mov...
907
  				print_object = true;
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
908
909
910
  			break;
  
  		default:
d5f34153e   Waiman Long   debugobjects: Mov...
911
  			print_object = true;
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
912
913
  			break;
  		}
d5f34153e   Waiman Long   debugobjects: Mov...
914
915
916
917
  	}
  
  	raw_spin_unlock_irqrestore(&db->lock, flags);
  	if (!obj) {
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
918
919
920
921
922
  		struct debug_obj o = { .object = addr,
  				       .state = ODEBUG_STATE_NOTAVAILABLE,
  				       .descr = descr };
  
  		debug_print_object(&o, "active_state");
d5f34153e   Waiman Long   debugobjects: Mov...
923
924
  	} else if (print_object) {
  		debug_print_object(obj, "active_state");
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
925
  	}
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
926
  }
f8ff04e2b   Chris Wilson   lib/debugobjects:...
927
  EXPORT_SYMBOL_GPL(debug_object_active_state);
a5d8e467f   Mathieu Desnoyers   Debugobjects tran...
928

3ac7fe5a4   Thomas Gleixner   infrastructure to...
929
930
931
932
  #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;
aedcade6f   Stephen Boyd   debugobjects: All...
933
  	const struct debug_obj_descr *descr;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
934
935
  	enum debug_obj_state state;
  	struct debug_bucket *db;
1ea9b98b0   Yang Shi   debugobjects: Use...
936
  	struct hlist_node *tmp;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
937
  	struct debug_obj *obj;
bd9dcd046   Yang Shi   debugobjects: Exp...
938
  	int cnt, objs_checked = 0;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
939
940
941
942
943
944
945
946
947
948
949
950
  
  	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...
951
  		raw_spin_lock_irqsave(&db->lock, flags);
b67bfe0d4   Sasha Levin   hlist: drop the n...
952
  		hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
953
954
955
956
957
958
959
  			cnt++;
  			oaddr = (unsigned long) obj->object;
  			if (oaddr < saddr || oaddr >= eaddr)
  				continue;
  
  			switch (obj->state) {
  			case ODEBUG_STATE_ACTIVE:
3ac7fe5a4   Thomas Gleixner   infrastructure to...
960
961
  				descr = obj->descr;
  				state = obj->state;
aef9cb052   Thomas Gleixner   debugobjects: Con...
962
  				raw_spin_unlock_irqrestore(&db->lock, flags);
d5f34153e   Waiman Long   debugobjects: Mov...
963
  				debug_print_object(obj, "free");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
964
965
966
967
968
  				debug_object_fixup(descr->fixup_free,
  						   (void *) oaddr, state);
  				goto repeat;
  			default:
  				hlist_del(&obj->node);
a7344a68a   Waiman Long   debugobjects: Les...
969
  				__free_object(obj);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
970
971
972
  				break;
  			}
  		}
aef9cb052   Thomas Gleixner   debugobjects: Con...
973
  		raw_spin_unlock_irqrestore(&db->lock, flags);
673d62cc5   Vegard Nossum   debugobjects: fix...
974

3ac7fe5a4   Thomas Gleixner   infrastructure to...
975
976
  		if (cnt > debug_objects_maxchain)
  			debug_objects_maxchain = cnt;
bd9dcd046   Yang Shi   debugobjects: Exp...
977
978
  
  		objs_checked += cnt;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
979
  	}
bd9dcd046   Yang Shi   debugobjects: Exp...
980
981
982
  
  	if (objs_checked > debug_objects_maxchecked)
  		debug_objects_maxchecked = objs_checked;
1ea9b98b0   Yang Shi   debugobjects: Use...
983
984
  
  	/* Schedule work to actually kmem_cache_free() objects */
35fd7a637   Marco Elver   debugobjects: Fix...
985
  	if (!READ_ONCE(obj_freeing) && READ_ONCE(obj_nr_tofree)) {
a7344a68a   Waiman Long   debugobjects: Les...
986
987
988
  		WRITE_ONCE(obj_freeing, true);
  		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
  	}
3ac7fe5a4   Thomas Gleixner   infrastructure to...
989
990
991
992
993
994
995
996
997
998
999
1000
1001
  }
  
  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)
  {
d86998b17   Waiman Long   debugobjects: Add...
1002
1003
1004
1005
  	int cpu, obj_percpu_free = 0;
  
  	for_each_possible_cpu(cpu)
  		obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1006
1007
  	seq_printf(m, "max_chain     :%d
  ", debug_objects_maxchain);
bd9dcd046   Yang Shi   debugobjects: Exp...
1008
1009
  	seq_printf(m, "max_checked   :%d
  ", debug_objects_maxchecked);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1010
1011
1012
1013
  	seq_printf(m, "warnings      :%d
  ", debug_objects_warnings);
  	seq_printf(m, "fixups        :%d
  ", debug_objects_fixups);
35fd7a637   Marco Elver   debugobjects: Fix...
1014
1015
  	seq_printf(m, "pool_free     :%d
  ", READ_ONCE(obj_pool_free) + obj_percpu_free);
d86998b17   Waiman Long   debugobjects: Add...
1016
1017
  	seq_printf(m, "pool_pcp_free :%d
  ", obj_percpu_free);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1018
1019
  	seq_printf(m, "pool_min_free :%d
  ", obj_pool_min_free);
d86998b17   Waiman Long   debugobjects: Add...
1020
1021
  	seq_printf(m, "pool_used     :%d
  ", obj_pool_used - obj_percpu_free);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1022
1023
  	seq_printf(m, "pool_max_used :%d
  ", obj_pool_max_used);
35fd7a637   Marco Elver   debugobjects: Fix...
1024
1025
  	seq_printf(m, "on_free_list  :%d
  ", READ_ONCE(obj_nr_tofree));
0cad93c34   Waiman Long   debugobjects: Imp...
1026
1027
1028
1029
  	seq_printf(m, "objs_allocated:%d
  ", debug_objects_allocated);
  	seq_printf(m, "objs_freed    :%d
  ", debug_objects_freed);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1030
1031
  	return 0;
  }
0f85c4805   Qinglang Miao   debugobjects: Con...
1032
  DEFINE_SHOW_ATTRIBUTE(debug_stats);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1033
1034
1035
  
  static int __init debug_objects_init_debugfs(void)
  {
fecb0d95c   Greg Kroah-Hartman   debugobjects: No ...
1036
  	struct dentry *dbgdir;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1037
1038
1039
1040
1041
  
  	if (!debug_objects_enabled)
  		return 0;
  
  	dbgdir = debugfs_create_dir("debug_objects", NULL);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1042

fecb0d95c   Greg Kroah-Hartman   debugobjects: No ...
1043
  	debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1044
1045
  
  	return 0;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
  }
  __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];
  };
aedcade6f   Stephen Boyd   debugobjects: All...
1061
  static __initconst const struct debug_obj_descr descr_type_test;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1062

b9fdac7f6   Du, Changbin   debugobjects: ins...
1063
1064
1065
1066
1067
1068
  static bool __init is_static_object(void *addr)
  {
  	struct self_test *obj = addr;
  
  	return obj->static_init;
  }
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1069
1070
1071
1072
  /*
   * fixup_init is called when:
   * - an active object is initialized
   */
b1e4d9d82   Du, Changbin   debugobjects: mak...
1073
  static bool __init fixup_init(void *addr, enum debug_obj_state state)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1074
1075
1076
1077
1078
1079
1080
  {
  	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...
1081
  		return true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1082
  	default:
b1e4d9d82   Du, Changbin   debugobjects: mak...
1083
  		return false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1084
1085
1086
1087
1088
1089
  	}
  }
  
  /*
   * fixup_activate is called when:
   * - an active object is activated
b9fdac7f6   Du, Changbin   debugobjects: ins...
1090
   * - an unknown non-static object is activated
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1091
   */
b1e4d9d82   Du, Changbin   debugobjects: mak...
1092
  static bool __init fixup_activate(void *addr, enum debug_obj_state state)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1093
1094
1095
1096
1097
  {
  	struct self_test *obj = addr;
  
  	switch (state) {
  	case ODEBUG_STATE_NOTAVAILABLE:
b1e4d9d82   Du, Changbin   debugobjects: mak...
1098
  		return true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1099
1100
1101
  	case ODEBUG_STATE_ACTIVE:
  		debug_object_deactivate(obj, &descr_type_test);
  		debug_object_activate(obj, &descr_type_test);
b1e4d9d82   Du, Changbin   debugobjects: mak...
1102
  		return true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1103
1104
  
  	default:
b1e4d9d82   Du, Changbin   debugobjects: mak...
1105
  		return false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1106
1107
1108
1109
1110
1111
1112
  	}
  }
  
  /*
   * fixup_destroy is called when:
   * - an active object is destroyed
   */
b1e4d9d82   Du, Changbin   debugobjects: mak...
1113
  static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1114
1115
1116
1117
1118
1119
1120
  {
  	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...
1121
  		return true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1122
  	default:
b1e4d9d82   Du, Changbin   debugobjects: mak...
1123
  		return false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1124
1125
1126
1127
1128
1129
1130
  	}
  }
  
  /*
   * fixup_free is called when:
   * - an active object is freed
   */
b1e4d9d82   Du, Changbin   debugobjects: mak...
1131
  static bool __init fixup_free(void *addr, enum debug_obj_state state)
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1132
1133
1134
1135
1136
1137
1138
  {
  	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...
1139
  		return true;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1140
  	default:
b1e4d9d82   Du, Changbin   debugobjects: mak...
1141
  		return false;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1142
1143
  	}
  }
1fb2f77c0   Henrik Kretzschmar   debugobjects: Sec...
1144
  static int __init
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1145
1146
1147
1148
1149
1150
1151
1152
  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...
1153
  	raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1154
1155
1156
  
  	obj = lookup_object(addr, db);
  	if (!obj && state != ODEBUG_STATE_NONE) {
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
1157
1158
  		WARN(1, KERN_ERR "ODEBUG: selftest object not found
  ");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1159
1160
1161
  		goto out;
  	}
  	if (obj && obj->state != state) {
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
1162
1163
  		WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d
  ",
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1164
  		       obj->state, state);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1165
1166
1167
  		goto out;
  	}
  	if (fixups != debug_objects_fixups) {
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
1168
1169
  		WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d
  ",
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1170
  		       fixups, debug_objects_fixups);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1171
1172
1173
  		goto out;
  	}
  	if (warnings != debug_objects_warnings) {
5cd2b459d   Arjan van de Ven   Use WARN() in lib/
1174
1175
  		WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d
  ",
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1176
  		       warnings, debug_objects_warnings);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1177
1178
1179
1180
  		goto out;
  	}
  	res = 0;
  out:
aef9cb052   Thomas Gleixner   debugobjects: Con...
1181
  	raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1182
1183
1184
1185
  	if (res)
  		debug_objects_enabled = 0;
  	return res;
  }
aedcade6f   Stephen Boyd   debugobjects: All...
1186
  static __initconst const struct debug_obj_descr descr_type_test = {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1187
  	.name			= "selftest",
b9fdac7f6   Du, Changbin   debugobjects: ins...
1188
  	.is_static_object	= is_static_object,
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
  	.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...
1238
  	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
  		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....
1258
1259
  	pr_info("selftest passed
  ");
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
  
  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...
1282
  		raw_spin_lock_init(&obj_hash[i].lock);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1283
1284
1285
1286
1287
1288
  
  	for (i = 0; i < ODEBUG_POOL_SIZE; i++)
  		hlist_add_head(&obj_static_pool[i].node, &obj_pool);
  }
  
  /*
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1289
1290
   * Convert the statically allocated objects to dynamic ones:
   */
1fb2f77c0   Henrik Kretzschmar   debugobjects: Sec...
1291
  static int __init debug_objects_replace_static_objects(void)
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1292
1293
  {
  	struct debug_bucket *db = obj_hash;
b67bfe0d4   Sasha Levin   hlist: drop the n...
1294
  	struct hlist_node *tmp;
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
  	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);
  	}
  
  	/*
a9ee3a63d   Qian Cai   debugobjects: cal...
1307
1308
1309
  	 * debug_objects_mem_init() is now called early that only one CPU is up
  	 * and interrupts have been disabled, so it is safe to replace the
  	 * active object references.
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1310
  	 */
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1311
1312
  
  	/* Remove the statically allocated objects from the pool */
b67bfe0d4   Sasha Levin   hlist: drop the n...
1313
  	hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1314
1315
1316
1317
1318
1319
1320
  		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...
1321
  		hlist_for_each_entry(obj, &objects, node) {
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1322
1323
1324
1325
1326
1327
1328
1329
  			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++;
  		}
  	}
c0f35cc0b   Fabian Frederick   lib/debugobjects....
1330
1331
1332
  	pr_debug("%d of %d active objects replaced
  ",
  		 cnt, obj_pool_used);
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1333
1334
  	return 0;
  free:
b67bfe0d4   Sasha Levin   hlist: drop the n...
1335
  	hlist_for_each_entry_safe(obj, tmp, &objects, node) {
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1336
1337
1338
1339
1340
1341
1342
  		hlist_del(&obj->node);
  		kmem_cache_free(obj_cache, obj);
  	}
  	return -ENOMEM;
  }
  
  /*
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1343
1344
1345
1346
1347
1348
1349
   * 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)
  {
634d61f45   Waiman Long   debugobjects: Per...
1350
  	int cpu, extras;
d86998b17   Waiman Long   debugobjects: Add...
1351

3ac7fe5a4   Thomas Gleixner   infrastructure to...
1352
1353
  	if (!debug_objects_enabled)
  		return;
d86998b17   Waiman Long   debugobjects: Add...
1354
1355
1356
1357
1358
1359
1360
1361
  	/*
  	 * Initialize the percpu object pools
  	 *
  	 * Initialization is not strictly necessary, but was done for
  	 * completeness.
  	 */
  	for_each_possible_cpu(cpu)
  		INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool.free_objs, cpu));
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1362
1363
  	obj_cache = kmem_cache_create("debug_objects_cache",
  				      sizeof (struct debug_obj), 0,
8de456cf8   Qian Cai   debugobjects: avo...
1364
1365
  				      SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
  				      NULL);
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1366

1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1367
  	if (!obj_cache || debug_objects_replace_static_objects()) {
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1368
  		debug_objects_enabled = 0;
3ff4f80a7   Zhong Jiang   debugobjects: Rem...
1369
  		kmem_cache_destroy(obj_cache);
719e48439   Fabian Frederick   lib/debugobjects....
1370
1371
  		pr_warn("out of memory.
  ");
1be1cb7b4   Thomas Gleixner   debugobjects: rep...
1372
  	} else
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1373
  		debug_objects_selftest();
634d61f45   Waiman Long   debugobjects: Per...
1374

88451f2cd   Zqiang   debugobjects: Fre...
1375
1376
1377
1378
  #ifdef CONFIG_HOTPLUG_CPU
  	cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL,
  					object_cpu_offline);
  #endif
634d61f45   Waiman Long   debugobjects: Per...
1379
1380
1381
1382
1383
1384
1385
  	/*
  	 * Increase the thresholds for allocating and freeing objects
  	 * according to the number of possible CPUs available in the system.
  	 */
  	extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
  	debug_objects_pool_size += extras;
  	debug_objects_pool_min_level += extras;
3ac7fe5a4   Thomas Gleixner   infrastructure to...
1386
  }