blob: d78673e7dc568bb340a652c83777d0f66447d3fb [file] [log] [blame]
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001/*
2 * Generic infrastructure for lifetime debugging of objects.
3 *
4 * Started by Thomas Gleixner
5 *
6 * Copyright (C) 2008, Thomas Gleixner <tglx@linutronix.de>
7 *
8 * For licencing details see kernel-base/COPYING
9 */
Fabian Frederick719e4842014-06-04 16:06:04 -070010
11#define pr_fmt(fmt) "ODEBUG: " fmt
12
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070013#include <linux/debugobjects.h>
14#include <linux/interrupt.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040015#include <linux/sched.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070016#include <linux/seq_file.h>
17#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070019#include <linux/hash.h>
20
21#define ODEBUG_HASH_BITS 14
22#define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
23
Christian Borntraeger0b6ec8c2016-01-27 15:37:58 +010024#define ODEBUG_POOL_SIZE 1024
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070025#define ODEBUG_POOL_MIN_LEVEL 256
26
27#define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
28#define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
29#define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
30
31struct debug_bucket {
32 struct hlist_head list;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010033 raw_spinlock_t lock;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070034};
35
36static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
37
Thomas Gleixner1be1cb72009-03-16 18:53:18 +010038static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070039
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010040static DEFINE_RAW_SPINLOCK(pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070041
42static HLIST_HEAD(obj_pool);
43
44static int obj_pool_min_free = ODEBUG_POOL_SIZE;
45static int obj_pool_free = ODEBUG_POOL_SIZE;
46static int obj_pool_used;
47static int obj_pool_max_used;
48static struct kmem_cache *obj_cache;
49
50static int debug_objects_maxchain __read_mostly;
51static int debug_objects_fixups __read_mostly;
52static int debug_objects_warnings __read_mostly;
Ingo Molnar3ae70202008-11-26 10:02:00 +010053static int debug_objects_enabled __read_mostly
54 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
55
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070056static struct debug_obj_descr *descr_test __read_mostly;
57
Waiman Longc4b73aa2017-01-05 15:17:03 -050058/*
59 * Track numbers of kmem_cache_alloc and kmem_cache_free done.
60 */
61static int debug_objects_alloc;
62static int debug_objects_freed;
63
Thomas Gleixner337fff82009-03-16 10:04:53 +010064static void free_obj_work(struct work_struct *work);
65static DECLARE_WORK(debug_obj_work, free_obj_work);
66
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070067static int __init enable_object_debug(char *str)
68{
69 debug_objects_enabled = 1;
70 return 0;
71}
Kyle McMartin3e8ebb52009-03-01 20:41:41 -050072
73static int __init disable_object_debug(char *str)
74{
75 debug_objects_enabled = 0;
76 return 0;
77}
78
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070079early_param("debug_objects", enable_object_debug);
Kyle McMartin3e8ebb52009-03-01 20:41:41 -050080early_param("no_debug_objects", disable_object_debug);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070081
82static const char *obj_states[ODEBUG_STATE_MAX] = {
83 [ODEBUG_STATE_NONE] = "none",
84 [ODEBUG_STATE_INIT] = "initialized",
85 [ODEBUG_STATE_INACTIVE] = "inactive",
86 [ODEBUG_STATE_ACTIVE] = "active",
87 [ODEBUG_STATE_DESTROYED] = "destroyed",
88 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
89};
90
Thomas Gleixner1fda1072012-04-11 11:52:18 +020091static void fill_pool(void)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070092{
93 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
94 struct debug_obj *new;
Vegard Nossum50db04dd2008-06-15 00:47:36 +020095 unsigned long flags;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070096
97 if (likely(obj_pool_free >= ODEBUG_POOL_MIN_LEVEL))
Thomas Gleixner1fda1072012-04-11 11:52:18 +020098 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070099
100 if (unlikely(!obj_cache))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200101 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700102
103 while (obj_pool_free < ODEBUG_POOL_MIN_LEVEL) {
104
105 new = kmem_cache_zalloc(obj_cache, gfp);
106 if (!new)
Dan Carpenter33408082012-04-18 14:28:10 +0300107 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700108
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100109 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700110 hlist_add_head(&new->node, &obj_pool);
Waiman Longc4b73aa2017-01-05 15:17:03 -0500111 debug_objects_alloc++;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700112 obj_pool_free++;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100113 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700114 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700115}
116
117/*
118 * Lookup an object in the hash bucket.
119 */
120static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
121{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700122 struct debug_obj *obj;
123 int cnt = 0;
124
Sasha Levinb67bfe02013-02-27 17:06:00 -0800125 hlist_for_each_entry(obj, &b->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700126 cnt++;
127 if (obj->object == addr)
128 return obj;
129 }
130 if (cnt > debug_objects_maxchain)
131 debug_objects_maxchain = cnt;
132
133 return NULL;
134}
135
136/*
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200137 * Allocate a new object. If the pool is empty, switch off the debugger.
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200138 * Must be called with interrupts disabled.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700139 */
140static struct debug_obj *
141alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
142{
143 struct debug_obj *obj = NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700144
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100145 raw_spin_lock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700146 if (obj_pool.first) {
147 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
148
149 obj->object = addr;
150 obj->descr = descr;
151 obj->state = ODEBUG_STATE_NONE;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400152 obj->astate = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700153 hlist_del(&obj->node);
154
155 hlist_add_head(&obj->node, &b->list);
156
157 obj_pool_used++;
158 if (obj_pool_used > obj_pool_max_used)
159 obj_pool_max_used = obj_pool_used;
160
161 obj_pool_free--;
162 if (obj_pool_free < obj_pool_min_free)
163 obj_pool_min_free = obj_pool_free;
164 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100165 raw_spin_unlock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700166
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700167 return obj;
168}
169
170/*
Thomas Gleixner337fff82009-03-16 10:04:53 +0100171 * workqueue function to free objects.
172 */
173static void free_obj_work(struct work_struct *work)
174{
175 struct debug_obj *obj;
176 unsigned long flags;
177
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100178 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100179 while (obj_pool_free > ODEBUG_POOL_SIZE) {
180 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
181 hlist_del(&obj->node);
182 obj_pool_free--;
Waiman Longc4b73aa2017-01-05 15:17:03 -0500183 debug_objects_freed++;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100184 /*
185 * We release pool_lock across kmem_cache_free() to
186 * avoid contention on pool_lock.
187 */
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100188 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100189 kmem_cache_free(obj_cache, obj);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100190 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100191 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100192 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100193}
194
195/*
196 * Put the object back into the pool and schedule work to free objects
197 * if necessary.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700198 */
199static void free_object(struct debug_obj *obj)
200{
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200201 unsigned long flags;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100202 int sched = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700203
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100204 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100205 /*
206 * schedule work when the pool is filled and the cache is
207 * initialized:
208 */
209 if (obj_pool_free > ODEBUG_POOL_SIZE && obj_cache)
Tejun Heo7092dff2016-09-16 15:49:34 -0400210 sched = 1;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100211 hlist_add_head(&obj->node, &obj_pool);
212 obj_pool_free++;
213 obj_pool_used--;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100214 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100215 if (sched)
216 schedule_work(&debug_obj_work);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700217}
218
219/*
220 * We run out of memory. That means we probably have tons of objects
221 * allocated.
222 */
223static void debug_objects_oom(void)
224{
225 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800226 struct hlist_node *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200227 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700228 struct debug_obj *obj;
229 unsigned long flags;
230 int i;
231
Fabian Frederick719e4842014-06-04 16:06:04 -0700232 pr_warn("Out of memory. ODEBUG disabled\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700233
234 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100235 raw_spin_lock_irqsave(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200236 hlist_move_list(&db->list, &freelist);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100237 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200238
239 /* Now free them */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800240 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700241 hlist_del(&obj->node);
242 free_object(obj);
243 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700244 }
245}
246
247/*
248 * We use the pfn of the address for the hash. That way we can check
249 * for freed objects simply by checking the affected bucket.
250 */
251static struct debug_bucket *get_bucket(unsigned long addr)
252{
253 unsigned long hash;
254
255 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
256 return &obj_hash[hash];
257}
258
259static void debug_print_object(struct debug_obj *obj, char *msg)
260{
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100261 struct debug_obj_descr *descr = obj->descr;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700262 static int limit;
263
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100264 if (limit < 5 && descr != descr_test) {
265 void *hint = descr->debug_hint ?
266 descr->debug_hint(obj->object) : NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700267 limit++;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400268 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100269 "object type: %s hint: %pS\n",
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400270 msg, obj_states[obj->state], obj->astate,
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100271 descr->name, hint);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700272 }
273 debug_objects_warnings++;
274}
275
276/*
277 * Try to repair the damage, so we have a better chance to get useful
278 * debug output.
279 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700280static bool
281debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700282 void * addr, enum debug_obj_state state)
283{
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700284 if (fixup && fixup(addr, state)) {
285 debug_objects_fixups++;
286 return true;
287 }
288 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700289}
290
291static void debug_object_is_on_stack(void *addr, int onstack)
292{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700293 int is_on_stack;
294 static int limit;
295
296 if (limit > 4)
297 return;
298
FUJITA Tomonori8b05c7e2008-07-23 21:26:53 -0700299 is_on_stack = object_is_on_stack(addr);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700300 if (is_on_stack == onstack)
301 return;
302
303 limit++;
304 if (is_on_stack)
Fabian Frederick719e4842014-06-04 16:06:04 -0700305 pr_warn("object is on stack, but not annotated\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700306 else
Fabian Frederick719e4842014-06-04 16:06:04 -0700307 pr_warn("object is not on stack, but annotated\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700308 WARN_ON(1);
309}
310
311static void
312__debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
313{
314 enum debug_obj_state state;
315 struct debug_bucket *db;
316 struct debug_obj *obj;
317 unsigned long flags;
318
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200319 fill_pool();
320
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700321 db = get_bucket((unsigned long) addr);
322
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100323 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700324
325 obj = lookup_object(addr, db);
326 if (!obj) {
327 obj = alloc_object(addr, db, descr);
328 if (!obj) {
329 debug_objects_enabled = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100330 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700331 debug_objects_oom();
332 return;
333 }
334 debug_object_is_on_stack(addr, onstack);
335 }
336
337 switch (obj->state) {
338 case ODEBUG_STATE_NONE:
339 case ODEBUG_STATE_INIT:
340 case ODEBUG_STATE_INACTIVE:
341 obj->state = ODEBUG_STATE_INIT;
342 break;
343
344 case ODEBUG_STATE_ACTIVE:
345 debug_print_object(obj, "init");
346 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100347 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700348 debug_object_fixup(descr->fixup_init, addr, state);
349 return;
350
351 case ODEBUG_STATE_DESTROYED:
352 debug_print_object(obj, "init");
353 break;
354 default:
355 break;
356 }
357
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100358 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700359}
360
361/**
362 * debug_object_init - debug checks when an object is initialized
363 * @addr: address of the object
364 * @descr: pointer to an object specific debug description structure
365 */
366void debug_object_init(void *addr, struct debug_obj_descr *descr)
367{
368 if (!debug_objects_enabled)
369 return;
370
371 __debug_object_init(addr, descr, 0);
372}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800373EXPORT_SYMBOL_GPL(debug_object_init);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700374
375/**
376 * debug_object_init_on_stack - debug checks when an object on stack is
377 * initialized
378 * @addr: address of the object
379 * @descr: pointer to an object specific debug description structure
380 */
381void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
382{
383 if (!debug_objects_enabled)
384 return;
385
386 __debug_object_init(addr, descr, 1);
387}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800388EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700389
390/**
391 * debug_object_activate - debug checks when an object is activated
392 * @addr: address of the object
393 * @descr: pointer to an object specific debug description structure
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700394 * Returns 0 for success, -EINVAL for check failed.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700395 */
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700396int debug_object_activate(void *addr, struct debug_obj_descr *descr)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700397{
398 enum debug_obj_state state;
399 struct debug_bucket *db;
400 struct debug_obj *obj;
401 unsigned long flags;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700402 int ret;
Stephen Boydfeac18d2011-11-07 19:48:26 -0800403 struct debug_obj o = { .object = addr,
404 .state = ODEBUG_STATE_NOTAVAILABLE,
405 .descr = descr };
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700406
407 if (!debug_objects_enabled)
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700408 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700409
410 db = get_bucket((unsigned long) addr);
411
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100412 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700413
414 obj = lookup_object(addr, db);
415 if (obj) {
416 switch (obj->state) {
417 case ODEBUG_STATE_INIT:
418 case ODEBUG_STATE_INACTIVE:
419 obj->state = ODEBUG_STATE_ACTIVE;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700420 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700421 break;
422
423 case ODEBUG_STATE_ACTIVE:
424 debug_print_object(obj, "activate");
425 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100426 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700427 ret = debug_object_fixup(descr->fixup_activate, addr, state);
Du, Changbine7a8e782016-05-19 17:09:23 -0700428 return ret ? 0 : -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700429
430 case ODEBUG_STATE_DESTROYED:
431 debug_print_object(obj, "activate");
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700432 ret = -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700433 break;
434 default:
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700435 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700436 break;
437 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100438 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700439 return ret;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700440 }
441
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100442 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700443 /*
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700444 * We are here when a static object is activated. We
445 * let the type specific code confirm whether this is
446 * true or not. if true, we just make sure that the
447 * static object is tracked in the object tracker. If
448 * not, this must be a bug, so we try to fix it up.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700449 */
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700450 if (descr->is_static_object && descr->is_static_object(addr)) {
451 /* track this static object */
452 debug_object_init(addr, descr);
453 debug_object_activate(addr, descr);
454 } else {
Stephen Boydfeac18d2011-11-07 19:48:26 -0800455 debug_print_object(&o, "activate");
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700456 ret = debug_object_fixup(descr->fixup_activate, addr,
457 ODEBUG_STATE_NOTAVAILABLE);
458 return ret ? 0 : -EINVAL;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700459 }
460 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700461}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800462EXPORT_SYMBOL_GPL(debug_object_activate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700463
464/**
465 * debug_object_deactivate - debug checks when an object is deactivated
466 * @addr: address of the object
467 * @descr: pointer to an object specific debug description structure
468 */
469void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
470{
471 struct debug_bucket *db;
472 struct debug_obj *obj;
473 unsigned long flags;
474
475 if (!debug_objects_enabled)
476 return;
477
478 db = get_bucket((unsigned long) addr);
479
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100480 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700481
482 obj = lookup_object(addr, db);
483 if (obj) {
484 switch (obj->state) {
485 case ODEBUG_STATE_INIT:
486 case ODEBUG_STATE_INACTIVE:
487 case ODEBUG_STATE_ACTIVE:
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400488 if (!obj->astate)
489 obj->state = ODEBUG_STATE_INACTIVE;
490 else
491 debug_print_object(obj, "deactivate");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700492 break;
493
494 case ODEBUG_STATE_DESTROYED:
495 debug_print_object(obj, "deactivate");
496 break;
497 default:
498 break;
499 }
500 } else {
501 struct debug_obj o = { .object = addr,
502 .state = ODEBUG_STATE_NOTAVAILABLE,
503 .descr = descr };
504
505 debug_print_object(&o, "deactivate");
506 }
507
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100508 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700509}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800510EXPORT_SYMBOL_GPL(debug_object_deactivate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700511
512/**
513 * debug_object_destroy - debug checks when an object is destroyed
514 * @addr: address of the object
515 * @descr: pointer to an object specific debug description structure
516 */
517void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
518{
519 enum debug_obj_state state;
520 struct debug_bucket *db;
521 struct debug_obj *obj;
522 unsigned long flags;
523
524 if (!debug_objects_enabled)
525 return;
526
527 db = get_bucket((unsigned long) addr);
528
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100529 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700530
531 obj = lookup_object(addr, db);
532 if (!obj)
533 goto out_unlock;
534
535 switch (obj->state) {
536 case ODEBUG_STATE_NONE:
537 case ODEBUG_STATE_INIT:
538 case ODEBUG_STATE_INACTIVE:
539 obj->state = ODEBUG_STATE_DESTROYED;
540 break;
541 case ODEBUG_STATE_ACTIVE:
542 debug_print_object(obj, "destroy");
543 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100544 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700545 debug_object_fixup(descr->fixup_destroy, addr, state);
546 return;
547
548 case ODEBUG_STATE_DESTROYED:
549 debug_print_object(obj, "destroy");
550 break;
551 default:
552 break;
553 }
554out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100555 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700556}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800557EXPORT_SYMBOL_GPL(debug_object_destroy);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700558
559/**
560 * debug_object_free - debug checks when an object is freed
561 * @addr: address of the object
562 * @descr: pointer to an object specific debug description structure
563 */
564void debug_object_free(void *addr, struct debug_obj_descr *descr)
565{
566 enum debug_obj_state state;
567 struct debug_bucket *db;
568 struct debug_obj *obj;
569 unsigned long flags;
570
571 if (!debug_objects_enabled)
572 return;
573
574 db = get_bucket((unsigned long) addr);
575
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100576 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700577
578 obj = lookup_object(addr, db);
579 if (!obj)
580 goto out_unlock;
581
582 switch (obj->state) {
583 case ODEBUG_STATE_ACTIVE:
584 debug_print_object(obj, "free");
585 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100586 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700587 debug_object_fixup(descr->fixup_free, addr, state);
588 return;
589 default:
590 hlist_del(&obj->node);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100591 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700592 free_object(obj);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200593 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700594 }
595out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100596 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700597}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800598EXPORT_SYMBOL_GPL(debug_object_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700599
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400600/**
Christine Chanb84d4352011-11-07 19:48:27 -0800601 * debug_object_assert_init - debug checks when object should be init-ed
602 * @addr: address of the object
603 * @descr: pointer to an object specific debug description structure
604 */
605void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
606{
607 struct debug_bucket *db;
608 struct debug_obj *obj;
609 unsigned long flags;
610
611 if (!debug_objects_enabled)
612 return;
613
614 db = get_bucket((unsigned long) addr);
615
616 raw_spin_lock_irqsave(&db->lock, flags);
617
618 obj = lookup_object(addr, db);
619 if (!obj) {
620 struct debug_obj o = { .object = addr,
621 .state = ODEBUG_STATE_NOTAVAILABLE,
622 .descr = descr };
623
624 raw_spin_unlock_irqrestore(&db->lock, flags);
625 /*
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700626 * Maybe the object is static, and we let the type specific
627 * code confirm. Track this static object if true, else invoke
628 * fixup.
Christine Chanb84d4352011-11-07 19:48:27 -0800629 */
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700630 if (descr->is_static_object && descr->is_static_object(addr)) {
631 /* Track this static object */
632 debug_object_init(addr, descr);
633 } else {
Christine Chanb84d4352011-11-07 19:48:27 -0800634 debug_print_object(&o, "assert_init");
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700635 debug_object_fixup(descr->fixup_assert_init, addr,
636 ODEBUG_STATE_NOTAVAILABLE);
637 }
Christine Chanb84d4352011-11-07 19:48:27 -0800638 return;
639 }
640
641 raw_spin_unlock_irqrestore(&db->lock, flags);
642}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800643EXPORT_SYMBOL_GPL(debug_object_assert_init);
Christine Chanb84d4352011-11-07 19:48:27 -0800644
645/**
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400646 * debug_object_active_state - debug checks object usage state machine
647 * @addr: address of the object
648 * @descr: pointer to an object specific debug description structure
649 * @expect: expected state
650 * @next: state to move to if expected state is found
651 */
652void
653debug_object_active_state(void *addr, struct debug_obj_descr *descr,
654 unsigned int expect, unsigned int next)
655{
656 struct debug_bucket *db;
657 struct debug_obj *obj;
658 unsigned long flags;
659
660 if (!debug_objects_enabled)
661 return;
662
663 db = get_bucket((unsigned long) addr);
664
665 raw_spin_lock_irqsave(&db->lock, flags);
666
667 obj = lookup_object(addr, db);
668 if (obj) {
669 switch (obj->state) {
670 case ODEBUG_STATE_ACTIVE:
671 if (obj->astate == expect)
672 obj->astate = next;
673 else
674 debug_print_object(obj, "active_state");
675 break;
676
677 default:
678 debug_print_object(obj, "active_state");
679 break;
680 }
681 } else {
682 struct debug_obj o = { .object = addr,
683 .state = ODEBUG_STATE_NOTAVAILABLE,
684 .descr = descr };
685
686 debug_print_object(&o, "active_state");
687 }
688
689 raw_spin_unlock_irqrestore(&db->lock, flags);
690}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800691EXPORT_SYMBOL_GPL(debug_object_active_state);
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400692
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700693#ifdef CONFIG_DEBUG_OBJECTS_FREE
694static void __debug_check_no_obj_freed(const void *address, unsigned long size)
695{
696 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800697 struct hlist_node *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200698 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700699 struct debug_obj_descr *descr;
700 enum debug_obj_state state;
701 struct debug_bucket *db;
702 struct debug_obj *obj;
703 int cnt;
704
705 saddr = (unsigned long) address;
706 eaddr = saddr + size;
707 paddr = saddr & ODEBUG_CHUNK_MASK;
708 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
709 chunks >>= ODEBUG_CHUNK_SHIFT;
710
711 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
712 db = get_bucket(paddr);
713
714repeat:
715 cnt = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100716 raw_spin_lock_irqsave(&db->lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800717 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700718 cnt++;
719 oaddr = (unsigned long) obj->object;
720 if (oaddr < saddr || oaddr >= eaddr)
721 continue;
722
723 switch (obj->state) {
724 case ODEBUG_STATE_ACTIVE:
725 debug_print_object(obj, "free");
726 descr = obj->descr;
727 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100728 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700729 debug_object_fixup(descr->fixup_free,
730 (void *) oaddr, state);
731 goto repeat;
732 default:
733 hlist_del(&obj->node);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200734 hlist_add_head(&obj->node, &freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700735 break;
736 }
737 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100738 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200739
740 /* Now free them */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800741 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200742 hlist_del(&obj->node);
743 free_object(obj);
744 }
745
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700746 if (cnt > debug_objects_maxchain)
747 debug_objects_maxchain = cnt;
748 }
749}
750
751void debug_check_no_obj_freed(const void *address, unsigned long size)
752{
753 if (debug_objects_enabled)
754 __debug_check_no_obj_freed(address, size);
755}
756#endif
757
758#ifdef CONFIG_DEBUG_FS
759
760static int debug_stats_show(struct seq_file *m, void *v)
761{
762 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
763 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
764 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
765 seq_printf(m, "pool_free :%d\n", obj_pool_free);
766 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
767 seq_printf(m, "pool_used :%d\n", obj_pool_used);
768 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
Waiman Longc4b73aa2017-01-05 15:17:03 -0500769 seq_printf(m, "objects_alloc :%d\n", debug_objects_alloc);
770 seq_printf(m, "objects_freed :%d\n", debug_objects_freed);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700771 return 0;
772}
773
774static int debug_stats_open(struct inode *inode, struct file *filp)
775{
776 return single_open(filp, debug_stats_show, NULL);
777}
778
779static const struct file_operations debug_stats_fops = {
780 .open = debug_stats_open,
781 .read = seq_read,
782 .llseek = seq_lseek,
783 .release = single_release,
784};
785
786static int __init debug_objects_init_debugfs(void)
787{
788 struct dentry *dbgdir, *dbgstats;
789
790 if (!debug_objects_enabled)
791 return 0;
792
793 dbgdir = debugfs_create_dir("debug_objects", NULL);
794 if (!dbgdir)
795 return -ENOMEM;
796
797 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
798 &debug_stats_fops);
799 if (!dbgstats)
800 goto err;
801
802 return 0;
803
804err:
805 debugfs_remove(dbgdir);
806
807 return -ENOMEM;
808}
809__initcall(debug_objects_init_debugfs);
810
811#else
812static inline void debug_objects_init_debugfs(void) { }
813#endif
814
815#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
816
817/* Random data structure for the self test */
818struct self_test {
819 unsigned long dummy1[6];
820 int static_init;
821 unsigned long dummy2[3];
822};
823
824static __initdata struct debug_obj_descr descr_type_test;
825
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700826static bool __init is_static_object(void *addr)
827{
828 struct self_test *obj = addr;
829
830 return obj->static_init;
831}
832
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700833/*
834 * fixup_init is called when:
835 * - an active object is initialized
836 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700837static bool __init fixup_init(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700838{
839 struct self_test *obj = addr;
840
841 switch (state) {
842 case ODEBUG_STATE_ACTIVE:
843 debug_object_deactivate(obj, &descr_type_test);
844 debug_object_init(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700845 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700846 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700847 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700848 }
849}
850
851/*
852 * fixup_activate is called when:
853 * - an active object is activated
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700854 * - an unknown non-static object is activated
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700855 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700856static bool __init fixup_activate(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700857{
858 struct self_test *obj = addr;
859
860 switch (state) {
861 case ODEBUG_STATE_NOTAVAILABLE:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700862 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700863 case ODEBUG_STATE_ACTIVE:
864 debug_object_deactivate(obj, &descr_type_test);
865 debug_object_activate(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700866 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700867
868 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700869 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700870 }
871}
872
873/*
874 * fixup_destroy is called when:
875 * - an active object is destroyed
876 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700877static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700878{
879 struct self_test *obj = addr;
880
881 switch (state) {
882 case ODEBUG_STATE_ACTIVE:
883 debug_object_deactivate(obj, &descr_type_test);
884 debug_object_destroy(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700885 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700886 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700887 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700888 }
889}
890
891/*
892 * fixup_free is called when:
893 * - an active object is freed
894 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700895static bool __init fixup_free(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700896{
897 struct self_test *obj = addr;
898
899 switch (state) {
900 case ODEBUG_STATE_ACTIVE:
901 debug_object_deactivate(obj, &descr_type_test);
902 debug_object_free(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700903 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700904 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700905 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700906 }
907}
908
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +0100909static int __init
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700910check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
911{
912 struct debug_bucket *db;
913 struct debug_obj *obj;
914 unsigned long flags;
915 int res = -EINVAL;
916
917 db = get_bucket((unsigned long) addr);
918
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100919 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700920
921 obj = lookup_object(addr, db);
922 if (!obj && state != ODEBUG_STATE_NONE) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700923 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700924 goto out;
925 }
926 if (obj && obj->state != state) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700927 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700928 obj->state, state);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700929 goto out;
930 }
931 if (fixups != debug_objects_fixups) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700932 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700933 fixups, debug_objects_fixups);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700934 goto out;
935 }
936 if (warnings != debug_objects_warnings) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700937 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700938 warnings, debug_objects_warnings);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700939 goto out;
940 }
941 res = 0;
942out:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100943 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700944 if (res)
945 debug_objects_enabled = 0;
946 return res;
947}
948
949static __initdata struct debug_obj_descr descr_type_test = {
950 .name = "selftest",
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700951 .is_static_object = is_static_object,
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700952 .fixup_init = fixup_init,
953 .fixup_activate = fixup_activate,
954 .fixup_destroy = fixup_destroy,
955 .fixup_free = fixup_free,
956};
957
958static __initdata struct self_test obj = { .static_init = 0 };
959
960static void __init debug_objects_selftest(void)
961{
962 int fixups, oldfixups, warnings, oldwarnings;
963 unsigned long flags;
964
965 local_irq_save(flags);
966
967 fixups = oldfixups = debug_objects_fixups;
968 warnings = oldwarnings = debug_objects_warnings;
969 descr_test = &descr_type_test;
970
971 debug_object_init(&obj, &descr_type_test);
972 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
973 goto out;
974 debug_object_activate(&obj, &descr_type_test);
975 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
976 goto out;
977 debug_object_activate(&obj, &descr_type_test);
978 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
979 goto out;
980 debug_object_deactivate(&obj, &descr_type_test);
981 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
982 goto out;
983 debug_object_destroy(&obj, &descr_type_test);
984 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
985 goto out;
986 debug_object_init(&obj, &descr_type_test);
987 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
988 goto out;
989 debug_object_activate(&obj, &descr_type_test);
990 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
991 goto out;
992 debug_object_deactivate(&obj, &descr_type_test);
993 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
994 goto out;
995 debug_object_free(&obj, &descr_type_test);
996 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
997 goto out;
998
999 obj.static_init = 1;
1000 debug_object_activate(&obj, &descr_type_test);
Stephen Boyd9f78ff02012-03-05 14:59:17 -08001001 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001002 goto out;
1003 debug_object_init(&obj, &descr_type_test);
1004 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1005 goto out;
1006 debug_object_free(&obj, &descr_type_test);
1007 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1008 goto out;
1009
1010#ifdef CONFIG_DEBUG_OBJECTS_FREE
1011 debug_object_init(&obj, &descr_type_test);
1012 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1013 goto out;
1014 debug_object_activate(&obj, &descr_type_test);
1015 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1016 goto out;
1017 __debug_check_no_obj_freed(&obj, sizeof(obj));
1018 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1019 goto out;
1020#endif
Fabian Frederick719e4842014-06-04 16:06:04 -07001021 pr_info("selftest passed\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001022
1023out:
1024 debug_objects_fixups = oldfixups;
1025 debug_objects_warnings = oldwarnings;
1026 descr_test = NULL;
1027
1028 local_irq_restore(flags);
1029}
1030#else
1031static inline void debug_objects_selftest(void) { }
1032#endif
1033
1034/*
1035 * Called during early boot to initialize the hash buckets and link
1036 * the static object pool objects into the poll list. After this call
1037 * the object tracker is fully operational.
1038 */
1039void __init debug_objects_early_init(void)
1040{
1041 int i;
1042
1043 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001044 raw_spin_lock_init(&obj_hash[i].lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001045
1046 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1047 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1048}
1049
1050/*
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001051 * Convert the statically allocated objects to dynamic ones:
1052 */
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001053static int __init debug_objects_replace_static_objects(void)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001054{
1055 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001056 struct hlist_node *tmp;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001057 struct debug_obj *obj, *new;
1058 HLIST_HEAD(objects);
1059 int i, cnt = 0;
1060
1061 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1062 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1063 if (!obj)
1064 goto free;
1065 hlist_add_head(&obj->node, &objects);
1066 }
1067
1068 /*
1069 * When debug_objects_mem_init() is called we know that only
1070 * one CPU is up, so disabling interrupts is enough
1071 * protection. This avoids the lockdep hell of lock ordering.
1072 */
1073 local_irq_disable();
1074
1075 /* Remove the statically allocated objects from the pool */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001076 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001077 hlist_del(&obj->node);
1078 /* Move the allocated objects to the pool */
1079 hlist_move_list(&objects, &obj_pool);
1080
1081 /* Replace the active object references */
1082 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1083 hlist_move_list(&db->list, &objects);
1084
Sasha Levinb67bfe02013-02-27 17:06:00 -08001085 hlist_for_each_entry(obj, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001086 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1087 hlist_del(&new->node);
1088 /* copy object data */
1089 *new = *obj;
1090 hlist_add_head(&new->node, &db->list);
1091 cnt++;
1092 }
1093 }
Thomas Gleixner765a5e02012-04-11 11:54:27 +02001094 local_irq_enable();
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001095
Fabian Frederickc0f35cc2014-06-04 16:06:05 -07001096 pr_debug("%d of %d active objects replaced\n",
1097 cnt, obj_pool_used);
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001098 return 0;
1099free:
Sasha Levinb67bfe02013-02-27 17:06:00 -08001100 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001101 hlist_del(&obj->node);
1102 kmem_cache_free(obj_cache, obj);
1103 }
1104 return -ENOMEM;
1105}
1106
1107/*
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001108 * Called after the kmem_caches are functional to setup a dedicated
1109 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1110 * prevents that the debug code is called on kmem_cache_free() for the
1111 * debug tracker objects to avoid recursive calls.
1112 */
1113void __init debug_objects_mem_init(void)
1114{
1115 if (!debug_objects_enabled)
1116 return;
1117
1118 obj_cache = kmem_cache_create("debug_objects_cache",
1119 sizeof (struct debug_obj), 0,
1120 SLAB_DEBUG_OBJECTS, NULL);
1121
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001122 if (!obj_cache || debug_objects_replace_static_objects()) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001123 debug_objects_enabled = 0;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001124 if (obj_cache)
1125 kmem_cache_destroy(obj_cache);
Fabian Frederick719e4842014-06-04 16:06:04 -07001126 pr_warn("out of memory.\n");
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001127 } else
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001128 debug_objects_selftest();
1129}