blob: 8c28cbd7e104b6b23dd9b87e25aac2f0b6796c44 [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;
Waiman Long97dd5522017-01-05 15:17:04 -050055static int debug_objects_pool_size __read_mostly
56 = ODEBUG_POOL_SIZE;
57static int debug_objects_pool_min_level __read_mostly
58 = ODEBUG_POOL_MIN_LEVEL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070059static struct debug_obj_descr *descr_test __read_mostly;
60
Waiman Longc4b73aa2017-01-05 15:17:03 -050061/*
Waiman Long0cad93c2017-02-07 16:40:30 -050062 * Track numbers of kmem_cache_alloc()/free() calls done.
Waiman Longc4b73aa2017-01-05 15:17:03 -050063 */
Waiman Long0cad93c2017-02-07 16:40:30 -050064static int debug_objects_allocated;
Waiman Longc4b73aa2017-01-05 15:17:03 -050065static int debug_objects_freed;
66
Thomas Gleixner337fff82009-03-16 10:04:53 +010067static void free_obj_work(struct work_struct *work);
68static DECLARE_WORK(debug_obj_work, free_obj_work);
69
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070070static int __init enable_object_debug(char *str)
71{
72 debug_objects_enabled = 1;
73 return 0;
74}
Kyle McMartin3e8ebb52009-03-01 20:41:41 -050075
76static int __init disable_object_debug(char *str)
77{
78 debug_objects_enabled = 0;
79 return 0;
80}
81
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070082early_param("debug_objects", enable_object_debug);
Kyle McMartin3e8ebb52009-03-01 20:41:41 -050083early_param("no_debug_objects", disable_object_debug);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070084
85static const char *obj_states[ODEBUG_STATE_MAX] = {
86 [ODEBUG_STATE_NONE] = "none",
87 [ODEBUG_STATE_INIT] = "initialized",
88 [ODEBUG_STATE_INACTIVE] = "inactive",
89 [ODEBUG_STATE_ACTIVE] = "active",
90 [ODEBUG_STATE_DESTROYED] = "destroyed",
91 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
92};
93
Thomas Gleixner1fda1072012-04-11 11:52:18 +020094static void fill_pool(void)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070095{
96 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
97 struct debug_obj *new;
Vegard Nossum50db04dd2008-06-15 00:47:36 +020098 unsigned long flags;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070099
Waiman Long97dd5522017-01-05 15:17:04 -0500100 if (likely(obj_pool_free >= debug_objects_pool_min_level))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200101 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700102
103 if (unlikely(!obj_cache))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200104 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700105
Waiman Long97dd5522017-01-05 15:17:04 -0500106 while (obj_pool_free < debug_objects_pool_min_level) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700107
108 new = kmem_cache_zalloc(obj_cache, gfp);
109 if (!new)
Dan Carpenter33408082012-04-18 14:28:10 +0300110 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700111
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100112 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700113 hlist_add_head(&new->node, &obj_pool);
Waiman Long0cad93c2017-02-07 16:40:30 -0500114 debug_objects_allocated++;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700115 obj_pool_free++;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100116 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700117 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700118}
119
120/*
121 * Lookup an object in the hash bucket.
122 */
123static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
124{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700125 struct debug_obj *obj;
126 int cnt = 0;
127
Sasha Levinb67bfe02013-02-27 17:06:00 -0800128 hlist_for_each_entry(obj, &b->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700129 cnt++;
130 if (obj->object == addr)
131 return obj;
132 }
133 if (cnt > debug_objects_maxchain)
134 debug_objects_maxchain = cnt;
135
136 return NULL;
137}
138
139/*
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200140 * Allocate a new object. If the pool is empty, switch off the debugger.
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200141 * Must be called with interrupts disabled.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700142 */
143static struct debug_obj *
144alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
145{
146 struct debug_obj *obj = NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700147
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100148 raw_spin_lock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700149 if (obj_pool.first) {
150 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
151
152 obj->object = addr;
153 obj->descr = descr;
154 obj->state = ODEBUG_STATE_NONE;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400155 obj->astate = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700156 hlist_del(&obj->node);
157
158 hlist_add_head(&obj->node, &b->list);
159
160 obj_pool_used++;
161 if (obj_pool_used > obj_pool_max_used)
162 obj_pool_max_used = obj_pool_used;
163
164 obj_pool_free--;
165 if (obj_pool_free < obj_pool_min_free)
166 obj_pool_min_free = obj_pool_free;
167 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100168 raw_spin_unlock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700169
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700170 return obj;
171}
172
173/*
Thomas Gleixner337fff82009-03-16 10:04:53 +0100174 * workqueue function to free objects.
Waiman Long858274b2017-01-05 15:17:05 -0500175 *
176 * To reduce contention on the global pool_lock, the actual freeing of
177 * debug objects will be delayed if the pool_lock is busy. We also free
178 * the objects in a batch of 4 for each lock/unlock cycle.
Thomas Gleixner337fff82009-03-16 10:04:53 +0100179 */
Waiman Long858274b2017-01-05 15:17:05 -0500180#define ODEBUG_FREE_BATCH 4
181
Thomas Gleixner337fff82009-03-16 10:04:53 +0100182static void free_obj_work(struct work_struct *work)
183{
Waiman Long858274b2017-01-05 15:17:05 -0500184 struct debug_obj *objs[ODEBUG_FREE_BATCH];
Thomas Gleixner337fff82009-03-16 10:04:53 +0100185 unsigned long flags;
Waiman Long858274b2017-01-05 15:17:05 -0500186 int i;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100187
Waiman Long858274b2017-01-05 15:17:05 -0500188 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
189 return;
190 while (obj_pool_free >= debug_objects_pool_size + ODEBUG_FREE_BATCH) {
191 for (i = 0; i < ODEBUG_FREE_BATCH; i++) {
192 objs[i] = hlist_entry(obj_pool.first,
193 typeof(*objs[0]), node);
194 hlist_del(&objs[i]->node);
195 }
196
197 obj_pool_free -= ODEBUG_FREE_BATCH;
198 debug_objects_freed += ODEBUG_FREE_BATCH;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100199 /*
200 * We release pool_lock across kmem_cache_free() to
201 * avoid contention on pool_lock.
202 */
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100203 raw_spin_unlock_irqrestore(&pool_lock, flags);
Waiman Long858274b2017-01-05 15:17:05 -0500204 for (i = 0; i < ODEBUG_FREE_BATCH; i++)
205 kmem_cache_free(obj_cache, objs[i]);
206 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
207 return;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100208 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100209 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100210}
211
212/*
213 * Put the object back into the pool and schedule work to free objects
214 * if necessary.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700215 */
216static void free_object(struct debug_obj *obj)
217{
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200218 unsigned long flags;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100219 int sched = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700220
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100221 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100222 /*
223 * schedule work when the pool is filled and the cache is
224 * initialized:
225 */
Waiman Long97dd5522017-01-05 15:17:04 -0500226 if (obj_pool_free > debug_objects_pool_size && obj_cache)
Tejun Heo7092dff2016-09-16 15:49:34 -0400227 sched = 1;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100228 hlist_add_head(&obj->node, &obj_pool);
229 obj_pool_free++;
230 obj_pool_used--;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100231 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100232 if (sched)
233 schedule_work(&debug_obj_work);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700234}
235
236/*
237 * We run out of memory. That means we probably have tons of objects
238 * allocated.
239 */
240static void debug_objects_oom(void)
241{
242 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800243 struct hlist_node *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200244 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700245 struct debug_obj *obj;
246 unsigned long flags;
247 int i;
248
Fabian Frederick719e4842014-06-04 16:06:04 -0700249 pr_warn("Out of memory. ODEBUG disabled\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700250
251 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100252 raw_spin_lock_irqsave(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200253 hlist_move_list(&db->list, &freelist);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100254 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200255
256 /* Now free them */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800257 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700258 hlist_del(&obj->node);
259 free_object(obj);
260 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700261 }
262}
263
264/*
265 * We use the pfn of the address for the hash. That way we can check
266 * for freed objects simply by checking the affected bucket.
267 */
268static struct debug_bucket *get_bucket(unsigned long addr)
269{
270 unsigned long hash;
271
272 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
273 return &obj_hash[hash];
274}
275
276static void debug_print_object(struct debug_obj *obj, char *msg)
277{
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100278 struct debug_obj_descr *descr = obj->descr;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700279 static int limit;
280
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100281 if (limit < 5 && descr != descr_test) {
282 void *hint = descr->debug_hint ?
283 descr->debug_hint(obj->object) : NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700284 limit++;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400285 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100286 "object type: %s hint: %pS\n",
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400287 msg, obj_states[obj->state], obj->astate,
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100288 descr->name, hint);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700289 }
290 debug_objects_warnings++;
291}
292
293/*
294 * Try to repair the damage, so we have a better chance to get useful
295 * debug output.
296 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700297static bool
298debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700299 void * addr, enum debug_obj_state state)
300{
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700301 if (fixup && fixup(addr, state)) {
302 debug_objects_fixups++;
303 return true;
304 }
305 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700306}
307
308static void debug_object_is_on_stack(void *addr, int onstack)
309{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700310 int is_on_stack;
311 static int limit;
312
313 if (limit > 4)
314 return;
315
FUJITA Tomonori8b05c7e2008-07-23 21:26:53 -0700316 is_on_stack = object_is_on_stack(addr);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700317 if (is_on_stack == onstack)
318 return;
319
320 limit++;
321 if (is_on_stack)
Fabian Frederick719e4842014-06-04 16:06:04 -0700322 pr_warn("object is on stack, but not annotated\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700323 else
Fabian Frederick719e4842014-06-04 16:06:04 -0700324 pr_warn("object is not on stack, but annotated\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700325 WARN_ON(1);
326}
327
328static void
329__debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
330{
331 enum debug_obj_state state;
332 struct debug_bucket *db;
333 struct debug_obj *obj;
334 unsigned long flags;
335
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200336 fill_pool();
337
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700338 db = get_bucket((unsigned long) addr);
339
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100340 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700341
342 obj = lookup_object(addr, db);
343 if (!obj) {
344 obj = alloc_object(addr, db, descr);
345 if (!obj) {
346 debug_objects_enabled = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100347 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700348 debug_objects_oom();
349 return;
350 }
351 debug_object_is_on_stack(addr, onstack);
352 }
353
354 switch (obj->state) {
355 case ODEBUG_STATE_NONE:
356 case ODEBUG_STATE_INIT:
357 case ODEBUG_STATE_INACTIVE:
358 obj->state = ODEBUG_STATE_INIT;
359 break;
360
361 case ODEBUG_STATE_ACTIVE:
362 debug_print_object(obj, "init");
363 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100364 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700365 debug_object_fixup(descr->fixup_init, addr, state);
366 return;
367
368 case ODEBUG_STATE_DESTROYED:
369 debug_print_object(obj, "init");
370 break;
371 default:
372 break;
373 }
374
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100375 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700376}
377
378/**
379 * debug_object_init - debug checks when an object is initialized
380 * @addr: address of the object
381 * @descr: pointer to an object specific debug description structure
382 */
383void debug_object_init(void *addr, struct debug_obj_descr *descr)
384{
385 if (!debug_objects_enabled)
386 return;
387
388 __debug_object_init(addr, descr, 0);
389}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800390EXPORT_SYMBOL_GPL(debug_object_init);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700391
392/**
393 * debug_object_init_on_stack - debug checks when an object on stack is
394 * initialized
395 * @addr: address of the object
396 * @descr: pointer to an object specific debug description structure
397 */
398void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
399{
400 if (!debug_objects_enabled)
401 return;
402
403 __debug_object_init(addr, descr, 1);
404}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800405EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700406
407/**
408 * debug_object_activate - debug checks when an object is activated
409 * @addr: address of the object
410 * @descr: pointer to an object specific debug description structure
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700411 * Returns 0 for success, -EINVAL for check failed.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700412 */
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700413int debug_object_activate(void *addr, struct debug_obj_descr *descr)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700414{
415 enum debug_obj_state state;
416 struct debug_bucket *db;
417 struct debug_obj *obj;
418 unsigned long flags;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700419 int ret;
Stephen Boydfeac18d2011-11-07 19:48:26 -0800420 struct debug_obj o = { .object = addr,
421 .state = ODEBUG_STATE_NOTAVAILABLE,
422 .descr = descr };
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700423
424 if (!debug_objects_enabled)
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700425 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700426
427 db = get_bucket((unsigned long) addr);
428
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100429 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700430
431 obj = lookup_object(addr, db);
432 if (obj) {
433 switch (obj->state) {
434 case ODEBUG_STATE_INIT:
435 case ODEBUG_STATE_INACTIVE:
436 obj->state = ODEBUG_STATE_ACTIVE;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700437 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700438 break;
439
440 case ODEBUG_STATE_ACTIVE:
441 debug_print_object(obj, "activate");
442 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100443 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700444 ret = debug_object_fixup(descr->fixup_activate, addr, state);
Du, Changbine7a8e782016-05-19 17:09:23 -0700445 return ret ? 0 : -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700446
447 case ODEBUG_STATE_DESTROYED:
448 debug_print_object(obj, "activate");
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700449 ret = -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700450 break;
451 default:
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700452 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700453 break;
454 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100455 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700456 return ret;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700457 }
458
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100459 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700460 /*
Du, Changbinb9fdac72016-05-19 17:09:41 -0700461 * We are here when a static object is activated. We
462 * let the type specific code confirm whether this is
463 * true or not. if true, we just make sure that the
464 * static object is tracked in the object tracker. If
465 * not, this must be a bug, so we try to fix it up.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700466 */
Du, Changbinb9fdac72016-05-19 17:09:41 -0700467 if (descr->is_static_object && descr->is_static_object(addr)) {
468 /* track this static object */
469 debug_object_init(addr, descr);
470 debug_object_activate(addr, descr);
471 } else {
Stephen Boydfeac18d2011-11-07 19:48:26 -0800472 debug_print_object(&o, "activate");
Du, Changbinb9fdac72016-05-19 17:09:41 -0700473 ret = debug_object_fixup(descr->fixup_activate, addr,
474 ODEBUG_STATE_NOTAVAILABLE);
475 return ret ? 0 : -EINVAL;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700476 }
477 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700478}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800479EXPORT_SYMBOL_GPL(debug_object_activate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700480
481/**
482 * debug_object_deactivate - debug checks when an object is deactivated
483 * @addr: address of the object
484 * @descr: pointer to an object specific debug description structure
485 */
486void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
487{
488 struct debug_bucket *db;
489 struct debug_obj *obj;
490 unsigned long flags;
491
492 if (!debug_objects_enabled)
493 return;
494
495 db = get_bucket((unsigned long) addr);
496
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100497 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700498
499 obj = lookup_object(addr, db);
500 if (obj) {
501 switch (obj->state) {
502 case ODEBUG_STATE_INIT:
503 case ODEBUG_STATE_INACTIVE:
504 case ODEBUG_STATE_ACTIVE:
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400505 if (!obj->astate)
506 obj->state = ODEBUG_STATE_INACTIVE;
507 else
508 debug_print_object(obj, "deactivate");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700509 break;
510
511 case ODEBUG_STATE_DESTROYED:
512 debug_print_object(obj, "deactivate");
513 break;
514 default:
515 break;
516 }
517 } else {
518 struct debug_obj o = { .object = addr,
519 .state = ODEBUG_STATE_NOTAVAILABLE,
520 .descr = descr };
521
522 debug_print_object(&o, "deactivate");
523 }
524
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100525 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700526}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800527EXPORT_SYMBOL_GPL(debug_object_deactivate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700528
529/**
530 * debug_object_destroy - debug checks when an object is destroyed
531 * @addr: address of the object
532 * @descr: pointer to an object specific debug description structure
533 */
534void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
535{
536 enum debug_obj_state state;
537 struct debug_bucket *db;
538 struct debug_obj *obj;
539 unsigned long flags;
540
541 if (!debug_objects_enabled)
542 return;
543
544 db = get_bucket((unsigned long) addr);
545
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100546 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700547
548 obj = lookup_object(addr, db);
549 if (!obj)
550 goto out_unlock;
551
552 switch (obj->state) {
553 case ODEBUG_STATE_NONE:
554 case ODEBUG_STATE_INIT:
555 case ODEBUG_STATE_INACTIVE:
556 obj->state = ODEBUG_STATE_DESTROYED;
557 break;
558 case ODEBUG_STATE_ACTIVE:
559 debug_print_object(obj, "destroy");
560 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100561 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700562 debug_object_fixup(descr->fixup_destroy, addr, state);
563 return;
564
565 case ODEBUG_STATE_DESTROYED:
566 debug_print_object(obj, "destroy");
567 break;
568 default:
569 break;
570 }
571out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100572 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700573}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800574EXPORT_SYMBOL_GPL(debug_object_destroy);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700575
576/**
577 * debug_object_free - debug checks when an object is freed
578 * @addr: address of the object
579 * @descr: pointer to an object specific debug description structure
580 */
581void debug_object_free(void *addr, struct debug_obj_descr *descr)
582{
583 enum debug_obj_state state;
584 struct debug_bucket *db;
585 struct debug_obj *obj;
586 unsigned long flags;
587
588 if (!debug_objects_enabled)
589 return;
590
591 db = get_bucket((unsigned long) addr);
592
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100593 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700594
595 obj = lookup_object(addr, db);
596 if (!obj)
597 goto out_unlock;
598
599 switch (obj->state) {
600 case ODEBUG_STATE_ACTIVE:
601 debug_print_object(obj, "free");
602 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100603 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700604 debug_object_fixup(descr->fixup_free, addr, state);
605 return;
606 default:
607 hlist_del(&obj->node);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100608 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700609 free_object(obj);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200610 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700611 }
612out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100613 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700614}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800615EXPORT_SYMBOL_GPL(debug_object_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700616
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400617/**
Christine Chanb84d4352011-11-07 19:48:27 -0800618 * debug_object_assert_init - debug checks when object should be init-ed
619 * @addr: address of the object
620 * @descr: pointer to an object specific debug description structure
621 */
622void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
623{
624 struct debug_bucket *db;
625 struct debug_obj *obj;
626 unsigned long flags;
627
628 if (!debug_objects_enabled)
629 return;
630
631 db = get_bucket((unsigned long) addr);
632
633 raw_spin_lock_irqsave(&db->lock, flags);
634
635 obj = lookup_object(addr, db);
636 if (!obj) {
637 struct debug_obj o = { .object = addr,
638 .state = ODEBUG_STATE_NOTAVAILABLE,
639 .descr = descr };
640
641 raw_spin_unlock_irqrestore(&db->lock, flags);
642 /*
Du, Changbinb9fdac72016-05-19 17:09:41 -0700643 * Maybe the object is static, and we let the type specific
644 * code confirm. Track this static object if true, else invoke
645 * fixup.
Christine Chanb84d4352011-11-07 19:48:27 -0800646 */
Du, Changbinb9fdac72016-05-19 17:09:41 -0700647 if (descr->is_static_object && descr->is_static_object(addr)) {
648 /* Track this static object */
649 debug_object_init(addr, descr);
650 } else {
Christine Chanb84d4352011-11-07 19:48:27 -0800651 debug_print_object(&o, "assert_init");
Du, Changbinb9fdac72016-05-19 17:09:41 -0700652 debug_object_fixup(descr->fixup_assert_init, addr,
653 ODEBUG_STATE_NOTAVAILABLE);
654 }
Christine Chanb84d4352011-11-07 19:48:27 -0800655 return;
656 }
657
658 raw_spin_unlock_irqrestore(&db->lock, flags);
659}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800660EXPORT_SYMBOL_GPL(debug_object_assert_init);
Christine Chanb84d4352011-11-07 19:48:27 -0800661
662/**
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400663 * debug_object_active_state - debug checks object usage state machine
664 * @addr: address of the object
665 * @descr: pointer to an object specific debug description structure
666 * @expect: expected state
667 * @next: state to move to if expected state is found
668 */
669void
670debug_object_active_state(void *addr, struct debug_obj_descr *descr,
671 unsigned int expect, unsigned int next)
672{
673 struct debug_bucket *db;
674 struct debug_obj *obj;
675 unsigned long flags;
676
677 if (!debug_objects_enabled)
678 return;
679
680 db = get_bucket((unsigned long) addr);
681
682 raw_spin_lock_irqsave(&db->lock, flags);
683
684 obj = lookup_object(addr, db);
685 if (obj) {
686 switch (obj->state) {
687 case ODEBUG_STATE_ACTIVE:
688 if (obj->astate == expect)
689 obj->astate = next;
690 else
691 debug_print_object(obj, "active_state");
692 break;
693
694 default:
695 debug_print_object(obj, "active_state");
696 break;
697 }
698 } else {
699 struct debug_obj o = { .object = addr,
700 .state = ODEBUG_STATE_NOTAVAILABLE,
701 .descr = descr };
702
703 debug_print_object(&o, "active_state");
704 }
705
706 raw_spin_unlock_irqrestore(&db->lock, flags);
707}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800708EXPORT_SYMBOL_GPL(debug_object_active_state);
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400709
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700710#ifdef CONFIG_DEBUG_OBJECTS_FREE
711static void __debug_check_no_obj_freed(const void *address, unsigned long size)
712{
713 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800714 struct hlist_node *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200715 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700716 struct debug_obj_descr *descr;
717 enum debug_obj_state state;
718 struct debug_bucket *db;
719 struct debug_obj *obj;
720 int cnt;
721
722 saddr = (unsigned long) address;
723 eaddr = saddr + size;
724 paddr = saddr & ODEBUG_CHUNK_MASK;
725 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
726 chunks >>= ODEBUG_CHUNK_SHIFT;
727
728 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
729 db = get_bucket(paddr);
730
731repeat:
732 cnt = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100733 raw_spin_lock_irqsave(&db->lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800734 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700735 cnt++;
736 oaddr = (unsigned long) obj->object;
737 if (oaddr < saddr || oaddr >= eaddr)
738 continue;
739
740 switch (obj->state) {
741 case ODEBUG_STATE_ACTIVE:
742 debug_print_object(obj, "free");
743 descr = obj->descr;
744 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100745 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700746 debug_object_fixup(descr->fixup_free,
747 (void *) oaddr, state);
748 goto repeat;
749 default:
750 hlist_del(&obj->node);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200751 hlist_add_head(&obj->node, &freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700752 break;
753 }
754 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100755 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200756
757 /* Now free them */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800758 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200759 hlist_del(&obj->node);
760 free_object(obj);
761 }
762
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700763 if (cnt > debug_objects_maxchain)
764 debug_objects_maxchain = cnt;
765 }
766}
767
768void debug_check_no_obj_freed(const void *address, unsigned long size)
769{
770 if (debug_objects_enabled)
771 __debug_check_no_obj_freed(address, size);
772}
773#endif
774
775#ifdef CONFIG_DEBUG_FS
776
777static int debug_stats_show(struct seq_file *m, void *v)
778{
779 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
780 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
781 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
782 seq_printf(m, "pool_free :%d\n", obj_pool_free);
783 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
784 seq_printf(m, "pool_used :%d\n", obj_pool_used);
785 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
Waiman Long0cad93c2017-02-07 16:40:30 -0500786 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
787 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700788 return 0;
789}
790
791static int debug_stats_open(struct inode *inode, struct file *filp)
792{
793 return single_open(filp, debug_stats_show, NULL);
794}
795
796static const struct file_operations debug_stats_fops = {
797 .open = debug_stats_open,
798 .read = seq_read,
799 .llseek = seq_lseek,
800 .release = single_release,
801};
802
803static int __init debug_objects_init_debugfs(void)
804{
805 struct dentry *dbgdir, *dbgstats;
806
807 if (!debug_objects_enabled)
808 return 0;
809
810 dbgdir = debugfs_create_dir("debug_objects", NULL);
811 if (!dbgdir)
812 return -ENOMEM;
813
814 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
815 &debug_stats_fops);
816 if (!dbgstats)
817 goto err;
818
819 return 0;
820
821err:
822 debugfs_remove(dbgdir);
823
824 return -ENOMEM;
825}
826__initcall(debug_objects_init_debugfs);
827
828#else
829static inline void debug_objects_init_debugfs(void) { }
830#endif
831
832#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
833
834/* Random data structure for the self test */
835struct self_test {
836 unsigned long dummy1[6];
837 int static_init;
838 unsigned long dummy2[3];
839};
840
841static __initdata struct debug_obj_descr descr_type_test;
842
Du, Changbinb9fdac72016-05-19 17:09:41 -0700843static bool __init is_static_object(void *addr)
844{
845 struct self_test *obj = addr;
846
847 return obj->static_init;
848}
849
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700850/*
851 * fixup_init is called when:
852 * - an active object is initialized
853 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700854static bool __init fixup_init(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700855{
856 struct self_test *obj = addr;
857
858 switch (state) {
859 case ODEBUG_STATE_ACTIVE:
860 debug_object_deactivate(obj, &descr_type_test);
861 debug_object_init(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700862 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700863 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700864 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700865 }
866}
867
868/*
869 * fixup_activate is called when:
870 * - an active object is activated
Du, Changbinb9fdac72016-05-19 17:09:41 -0700871 * - an unknown non-static object is activated
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700872 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700873static bool __init fixup_activate(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700874{
875 struct self_test *obj = addr;
876
877 switch (state) {
878 case ODEBUG_STATE_NOTAVAILABLE:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700879 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700880 case ODEBUG_STATE_ACTIVE:
881 debug_object_deactivate(obj, &descr_type_test);
882 debug_object_activate(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700883 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700884
885 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700886 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700887 }
888}
889
890/*
891 * fixup_destroy is called when:
892 * - an active object is destroyed
893 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700894static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700895{
896 struct self_test *obj = addr;
897
898 switch (state) {
899 case ODEBUG_STATE_ACTIVE:
900 debug_object_deactivate(obj, &descr_type_test);
901 debug_object_destroy(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700902 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700903 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700904 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700905 }
906}
907
908/*
909 * fixup_free is called when:
910 * - an active object is freed
911 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700912static bool __init fixup_free(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700913{
914 struct self_test *obj = addr;
915
916 switch (state) {
917 case ODEBUG_STATE_ACTIVE:
918 debug_object_deactivate(obj, &descr_type_test);
919 debug_object_free(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700920 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700921 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700922 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700923 }
924}
925
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +0100926static int __init
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700927check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
928{
929 struct debug_bucket *db;
930 struct debug_obj *obj;
931 unsigned long flags;
932 int res = -EINVAL;
933
934 db = get_bucket((unsigned long) addr);
935
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100936 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700937
938 obj = lookup_object(addr, db);
939 if (!obj && state != ODEBUG_STATE_NONE) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700940 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700941 goto out;
942 }
943 if (obj && obj->state != state) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700944 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700945 obj->state, state);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700946 goto out;
947 }
948 if (fixups != debug_objects_fixups) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700949 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700950 fixups, debug_objects_fixups);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700951 goto out;
952 }
953 if (warnings != debug_objects_warnings) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700954 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700955 warnings, debug_objects_warnings);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700956 goto out;
957 }
958 res = 0;
959out:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100960 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700961 if (res)
962 debug_objects_enabled = 0;
963 return res;
964}
965
966static __initdata struct debug_obj_descr descr_type_test = {
967 .name = "selftest",
Du, Changbinb9fdac72016-05-19 17:09:41 -0700968 .is_static_object = is_static_object,
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700969 .fixup_init = fixup_init,
970 .fixup_activate = fixup_activate,
971 .fixup_destroy = fixup_destroy,
972 .fixup_free = fixup_free,
973};
974
975static __initdata struct self_test obj = { .static_init = 0 };
976
977static void __init debug_objects_selftest(void)
978{
979 int fixups, oldfixups, warnings, oldwarnings;
980 unsigned long flags;
981
982 local_irq_save(flags);
983
984 fixups = oldfixups = debug_objects_fixups;
985 warnings = oldwarnings = debug_objects_warnings;
986 descr_test = &descr_type_test;
987
988 debug_object_init(&obj, &descr_type_test);
989 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
990 goto out;
991 debug_object_activate(&obj, &descr_type_test);
992 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
993 goto out;
994 debug_object_activate(&obj, &descr_type_test);
995 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
996 goto out;
997 debug_object_deactivate(&obj, &descr_type_test);
998 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
999 goto out;
1000 debug_object_destroy(&obj, &descr_type_test);
1001 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1002 goto out;
1003 debug_object_init(&obj, &descr_type_test);
1004 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1005 goto out;
1006 debug_object_activate(&obj, &descr_type_test);
1007 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1008 goto out;
1009 debug_object_deactivate(&obj, &descr_type_test);
1010 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1011 goto out;
1012 debug_object_free(&obj, &descr_type_test);
1013 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1014 goto out;
1015
1016 obj.static_init = 1;
1017 debug_object_activate(&obj, &descr_type_test);
Stephen Boyd9f78ff02012-03-05 14:59:17 -08001018 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001019 goto out;
1020 debug_object_init(&obj, &descr_type_test);
1021 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1022 goto out;
1023 debug_object_free(&obj, &descr_type_test);
1024 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1025 goto out;
1026
1027#ifdef CONFIG_DEBUG_OBJECTS_FREE
1028 debug_object_init(&obj, &descr_type_test);
1029 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1030 goto out;
1031 debug_object_activate(&obj, &descr_type_test);
1032 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1033 goto out;
1034 __debug_check_no_obj_freed(&obj, sizeof(obj));
1035 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1036 goto out;
1037#endif
Fabian Frederick719e4842014-06-04 16:06:04 -07001038 pr_info("selftest passed\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001039
1040out:
1041 debug_objects_fixups = oldfixups;
1042 debug_objects_warnings = oldwarnings;
1043 descr_test = NULL;
1044
1045 local_irq_restore(flags);
1046}
1047#else
1048static inline void debug_objects_selftest(void) { }
1049#endif
1050
1051/*
1052 * Called during early boot to initialize the hash buckets and link
1053 * the static object pool objects into the poll list. After this call
1054 * the object tracker is fully operational.
1055 */
1056void __init debug_objects_early_init(void)
1057{
1058 int i;
1059
1060 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001061 raw_spin_lock_init(&obj_hash[i].lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001062
1063 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1064 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1065}
1066
1067/*
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001068 * Convert the statically allocated objects to dynamic ones:
1069 */
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001070static int __init debug_objects_replace_static_objects(void)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001071{
1072 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001073 struct hlist_node *tmp;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001074 struct debug_obj *obj, *new;
1075 HLIST_HEAD(objects);
1076 int i, cnt = 0;
1077
1078 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1079 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1080 if (!obj)
1081 goto free;
1082 hlist_add_head(&obj->node, &objects);
1083 }
1084
1085 /*
1086 * When debug_objects_mem_init() is called we know that only
1087 * one CPU is up, so disabling interrupts is enough
1088 * protection. This avoids the lockdep hell of lock ordering.
1089 */
1090 local_irq_disable();
1091
1092 /* Remove the statically allocated objects from the pool */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001093 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001094 hlist_del(&obj->node);
1095 /* Move the allocated objects to the pool */
1096 hlist_move_list(&objects, &obj_pool);
1097
1098 /* Replace the active object references */
1099 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1100 hlist_move_list(&db->list, &objects);
1101
Sasha Levinb67bfe02013-02-27 17:06:00 -08001102 hlist_for_each_entry(obj, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001103 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1104 hlist_del(&new->node);
1105 /* copy object data */
1106 *new = *obj;
1107 hlist_add_head(&new->node, &db->list);
1108 cnt++;
1109 }
1110 }
Thomas Gleixner765a5e02012-04-11 11:54:27 +02001111 local_irq_enable();
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001112
Fabian Frederickc0f35cc2014-06-04 16:06:05 -07001113 pr_debug("%d of %d active objects replaced\n",
1114 cnt, obj_pool_used);
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001115 return 0;
1116free:
Sasha Levinb67bfe02013-02-27 17:06:00 -08001117 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001118 hlist_del(&obj->node);
1119 kmem_cache_free(obj_cache, obj);
1120 }
1121 return -ENOMEM;
1122}
1123
1124/*
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001125 * Called after the kmem_caches are functional to setup a dedicated
1126 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1127 * prevents that the debug code is called on kmem_cache_free() for the
1128 * debug tracker objects to avoid recursive calls.
1129 */
1130void __init debug_objects_mem_init(void)
1131{
1132 if (!debug_objects_enabled)
1133 return;
1134
1135 obj_cache = kmem_cache_create("debug_objects_cache",
1136 sizeof (struct debug_obj), 0,
1137 SLAB_DEBUG_OBJECTS, NULL);
1138
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001139 if (!obj_cache || debug_objects_replace_static_objects()) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001140 debug_objects_enabled = 0;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001141 if (obj_cache)
1142 kmem_cache_destroy(obj_cache);
Fabian Frederick719e4842014-06-04 16:06:04 -07001143 pr_warn("out of memory.\n");
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001144 } else
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001145 debug_objects_selftest();
Waiman Long97dd5522017-01-05 15:17:04 -05001146
1147 /*
1148 * Increase the thresholds for allocating and freeing objects
1149 * according to the number of possible CPUs available in the system.
1150 */
1151 debug_objects_pool_size += num_possible_cpus() * 32;
1152 debug_objects_pool_min_level += num_possible_cpus() * 4;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001153}