blob: f6d57a11c927b7a6db986bba56a7690d46147e86 [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>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010016#include <linux/sched/task_stack.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070017#include <linux/seq_file.h>
18#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070020#include <linux/hash.h>
Waiman Longcaba4cb2017-08-14 09:52:13 -040021#include <linux/kmemleak.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070022
23#define ODEBUG_HASH_BITS 14
24#define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
25
Christian Borntraeger0b6ec8c2016-01-27 15:37:58 +010026#define ODEBUG_POOL_SIZE 1024
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070027#define ODEBUG_POOL_MIN_LEVEL 256
28
29#define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
30#define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
31#define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
32
33struct debug_bucket {
34 struct hlist_head list;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010035 raw_spinlock_t lock;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070036};
37
38static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
39
Thomas Gleixner1be1cb72009-03-16 18:53:18 +010040static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070041
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010042static DEFINE_RAW_SPINLOCK(pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070043
44static HLIST_HEAD(obj_pool);
45
46static int obj_pool_min_free = ODEBUG_POOL_SIZE;
47static int obj_pool_free = ODEBUG_POOL_SIZE;
48static int obj_pool_used;
49static int obj_pool_max_used;
50static struct kmem_cache *obj_cache;
51
52static int debug_objects_maxchain __read_mostly;
Yang Shibd9dcd02018-02-06 07:18:25 +080053static int debug_objects_maxchecked __read_mostly;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070054static int debug_objects_fixups __read_mostly;
55static int debug_objects_warnings __read_mostly;
Ingo Molnar3ae70202008-11-26 10:02:00 +010056static int debug_objects_enabled __read_mostly
57 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
Waiman Long97dd5522017-01-05 15:17:04 -050058static int debug_objects_pool_size __read_mostly
59 = ODEBUG_POOL_SIZE;
60static int debug_objects_pool_min_level __read_mostly
61 = ODEBUG_POOL_MIN_LEVEL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070062static struct debug_obj_descr *descr_test __read_mostly;
63
Waiman Longc4b73aa2017-01-05 15:17:03 -050064/*
Waiman Long0cad93c2017-02-07 16:40:30 -050065 * Track numbers of kmem_cache_alloc()/free() calls done.
Waiman Longc4b73aa2017-01-05 15:17:03 -050066 */
Waiman Long0cad93c2017-02-07 16:40:30 -050067static int debug_objects_allocated;
Waiman Longc4b73aa2017-01-05 15:17:03 -050068static int debug_objects_freed;
69
Thomas Gleixner337fff82009-03-16 10:04:53 +010070static void free_obj_work(struct work_struct *work);
71static DECLARE_WORK(debug_obj_work, free_obj_work);
72
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070073static int __init enable_object_debug(char *str)
74{
75 debug_objects_enabled = 1;
76 return 0;
77}
Kyle McMartin3e8ebb52009-03-01 20:41:41 -050078
79static int __init disable_object_debug(char *str)
80{
81 debug_objects_enabled = 0;
82 return 0;
83}
84
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070085early_param("debug_objects", enable_object_debug);
Kyle McMartin3e8ebb52009-03-01 20:41:41 -050086early_param("no_debug_objects", disable_object_debug);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070087
88static const char *obj_states[ODEBUG_STATE_MAX] = {
89 [ODEBUG_STATE_NONE] = "none",
90 [ODEBUG_STATE_INIT] = "initialized",
91 [ODEBUG_STATE_INACTIVE] = "inactive",
92 [ODEBUG_STATE_ACTIVE] = "active",
93 [ODEBUG_STATE_DESTROYED] = "destroyed",
94 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
95};
96
Thomas Gleixner1fda1072012-04-11 11:52:18 +020097static void fill_pool(void)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070098{
99 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
100 struct debug_obj *new;
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200101 unsigned long flags;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700102
Waiman Long97dd5522017-01-05 15:17:04 -0500103 if (likely(obj_pool_free >= debug_objects_pool_min_level))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200104 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700105
106 if (unlikely(!obj_cache))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200107 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700108
Waiman Long97dd5522017-01-05 15:17:04 -0500109 while (obj_pool_free < debug_objects_pool_min_level) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700110
111 new = kmem_cache_zalloc(obj_cache, gfp);
112 if (!new)
Dan Carpenter33408082012-04-18 14:28:10 +0300113 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700114
Waiman Longcaba4cb2017-08-14 09:52:13 -0400115 kmemleak_ignore(new);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100116 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700117 hlist_add_head(&new->node, &obj_pool);
Waiman Long0cad93c2017-02-07 16:40:30 -0500118 debug_objects_allocated++;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700119 obj_pool_free++;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100120 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700121 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700122}
123
124/*
125 * Lookup an object in the hash bucket.
126 */
127static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
128{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700129 struct debug_obj *obj;
130 int cnt = 0;
131
Sasha Levinb67bfe02013-02-27 17:06:00 -0800132 hlist_for_each_entry(obj, &b->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700133 cnt++;
134 if (obj->object == addr)
135 return obj;
136 }
137 if (cnt > debug_objects_maxchain)
138 debug_objects_maxchain = cnt;
139
140 return NULL;
141}
142
143/*
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200144 * Allocate a new object. If the pool is empty, switch off the debugger.
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200145 * Must be called with interrupts disabled.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700146 */
147static struct debug_obj *
148alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
149{
150 struct debug_obj *obj = NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700151
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100152 raw_spin_lock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700153 if (obj_pool.first) {
154 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
155
156 obj->object = addr;
157 obj->descr = descr;
158 obj->state = ODEBUG_STATE_NONE;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400159 obj->astate = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700160 hlist_del(&obj->node);
161
162 hlist_add_head(&obj->node, &b->list);
163
164 obj_pool_used++;
165 if (obj_pool_used > obj_pool_max_used)
166 obj_pool_max_used = obj_pool_used;
167
168 obj_pool_free--;
169 if (obj_pool_free < obj_pool_min_free)
170 obj_pool_min_free = obj_pool_free;
171 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100172 raw_spin_unlock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700173
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700174 return obj;
175}
176
177/*
Thomas Gleixner337fff82009-03-16 10:04:53 +0100178 * workqueue function to free objects.
Waiman Long858274b2017-01-05 15:17:05 -0500179 *
180 * To reduce contention on the global pool_lock, the actual freeing of
181 * debug objects will be delayed if the pool_lock is busy. We also free
182 * the objects in a batch of 4 for each lock/unlock cycle.
Thomas Gleixner337fff82009-03-16 10:04:53 +0100183 */
Waiman Long858274b2017-01-05 15:17:05 -0500184#define ODEBUG_FREE_BATCH 4
185
Thomas Gleixner337fff82009-03-16 10:04:53 +0100186static void free_obj_work(struct work_struct *work)
187{
Waiman Long858274b2017-01-05 15:17:05 -0500188 struct debug_obj *objs[ODEBUG_FREE_BATCH];
Thomas Gleixner337fff82009-03-16 10:04:53 +0100189 unsigned long flags;
Waiman Long858274b2017-01-05 15:17:05 -0500190 int i;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100191
Waiman Long858274b2017-01-05 15:17:05 -0500192 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
193 return;
194 while (obj_pool_free >= debug_objects_pool_size + ODEBUG_FREE_BATCH) {
195 for (i = 0; i < ODEBUG_FREE_BATCH; i++) {
196 objs[i] = hlist_entry(obj_pool.first,
197 typeof(*objs[0]), node);
198 hlist_del(&objs[i]->node);
199 }
200
201 obj_pool_free -= ODEBUG_FREE_BATCH;
202 debug_objects_freed += ODEBUG_FREE_BATCH;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100203 /*
204 * We release pool_lock across kmem_cache_free() to
205 * avoid contention on pool_lock.
206 */
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100207 raw_spin_unlock_irqrestore(&pool_lock, flags);
Waiman Long858274b2017-01-05 15:17:05 -0500208 for (i = 0; i < ODEBUG_FREE_BATCH; i++)
209 kmem_cache_free(obj_cache, objs[i]);
210 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
211 return;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100212 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100213 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100214}
215
216/*
217 * Put the object back into the pool and schedule work to free objects
218 * if necessary.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700219 */
220static void free_object(struct debug_obj *obj)
221{
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200222 unsigned long flags;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100223 int sched = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700224
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100225 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100226 /*
227 * schedule work when the pool is filled and the cache is
228 * initialized:
229 */
Waiman Long97dd5522017-01-05 15:17:04 -0500230 if (obj_pool_free > debug_objects_pool_size && obj_cache)
Tejun Heo7092dff2016-09-16 15:49:34 -0400231 sched = 1;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100232 hlist_add_head(&obj->node, &obj_pool);
233 obj_pool_free++;
234 obj_pool_used--;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100235 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100236 if (sched)
237 schedule_work(&debug_obj_work);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700238}
239
240/*
241 * We run out of memory. That means we probably have tons of objects
242 * allocated.
243 */
244static void debug_objects_oom(void)
245{
246 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800247 struct hlist_node *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200248 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700249 struct debug_obj *obj;
250 unsigned long flags;
251 int i;
252
Fabian Frederick719e4842014-06-04 16:06:04 -0700253 pr_warn("Out of memory. ODEBUG disabled\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700254
255 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100256 raw_spin_lock_irqsave(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200257 hlist_move_list(&db->list, &freelist);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100258 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200259
260 /* Now free them */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800261 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700262 hlist_del(&obj->node);
263 free_object(obj);
264 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700265 }
266}
267
268/*
269 * We use the pfn of the address for the hash. That way we can check
270 * for freed objects simply by checking the affected bucket.
271 */
272static struct debug_bucket *get_bucket(unsigned long addr)
273{
274 unsigned long hash;
275
276 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
277 return &obj_hash[hash];
278}
279
280static void debug_print_object(struct debug_obj *obj, char *msg)
281{
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100282 struct debug_obj_descr *descr = obj->descr;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700283 static int limit;
284
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100285 if (limit < 5 && descr != descr_test) {
286 void *hint = descr->debug_hint ?
287 descr->debug_hint(obj->object) : NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700288 limit++;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400289 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100290 "object type: %s hint: %pS\n",
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400291 msg, obj_states[obj->state], obj->astate,
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100292 descr->name, hint);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700293 }
294 debug_objects_warnings++;
295}
296
297/*
298 * Try to repair the damage, so we have a better chance to get useful
299 * debug output.
300 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700301static bool
302debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700303 void * addr, enum debug_obj_state state)
304{
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700305 if (fixup && fixup(addr, state)) {
306 debug_objects_fixups++;
307 return true;
308 }
309 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700310}
311
312static void debug_object_is_on_stack(void *addr, int onstack)
313{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700314 int is_on_stack;
315 static int limit;
316
317 if (limit > 4)
318 return;
319
FUJITA Tomonori8b05c7e2008-07-23 21:26:53 -0700320 is_on_stack = object_is_on_stack(addr);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700321 if (is_on_stack == onstack)
322 return;
323
324 limit++;
325 if (is_on_stack)
Fabian Frederick719e4842014-06-04 16:06:04 -0700326 pr_warn("object is on stack, but not annotated\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700327 else
Fabian Frederick719e4842014-06-04 16:06:04 -0700328 pr_warn("object is not on stack, but annotated\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700329 WARN_ON(1);
330}
331
332static void
333__debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
334{
335 enum debug_obj_state state;
336 struct debug_bucket *db;
337 struct debug_obj *obj;
338 unsigned long flags;
339
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200340 fill_pool();
341
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700342 db = get_bucket((unsigned long) addr);
343
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100344 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700345
346 obj = lookup_object(addr, db);
347 if (!obj) {
348 obj = alloc_object(addr, db, descr);
349 if (!obj) {
350 debug_objects_enabled = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100351 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700352 debug_objects_oom();
353 return;
354 }
355 debug_object_is_on_stack(addr, onstack);
356 }
357
358 switch (obj->state) {
359 case ODEBUG_STATE_NONE:
360 case ODEBUG_STATE_INIT:
361 case ODEBUG_STATE_INACTIVE:
362 obj->state = ODEBUG_STATE_INIT;
363 break;
364
365 case ODEBUG_STATE_ACTIVE:
366 debug_print_object(obj, "init");
367 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100368 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700369 debug_object_fixup(descr->fixup_init, addr, state);
370 return;
371
372 case ODEBUG_STATE_DESTROYED:
373 debug_print_object(obj, "init");
374 break;
375 default:
376 break;
377 }
378
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100379 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700380}
381
382/**
383 * debug_object_init - debug checks when an object is initialized
384 * @addr: address of the object
385 * @descr: pointer to an object specific debug description structure
386 */
387void debug_object_init(void *addr, struct debug_obj_descr *descr)
388{
389 if (!debug_objects_enabled)
390 return;
391
392 __debug_object_init(addr, descr, 0);
393}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800394EXPORT_SYMBOL_GPL(debug_object_init);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700395
396/**
397 * debug_object_init_on_stack - debug checks when an object on stack is
398 * initialized
399 * @addr: address of the object
400 * @descr: pointer to an object specific debug description structure
401 */
402void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
403{
404 if (!debug_objects_enabled)
405 return;
406
407 __debug_object_init(addr, descr, 1);
408}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800409EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700410
411/**
412 * debug_object_activate - debug checks when an object is activated
413 * @addr: address of the object
414 * @descr: pointer to an object specific debug description structure
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700415 * Returns 0 for success, -EINVAL for check failed.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700416 */
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700417int debug_object_activate(void *addr, struct debug_obj_descr *descr)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700418{
419 enum debug_obj_state state;
420 struct debug_bucket *db;
421 struct debug_obj *obj;
422 unsigned long flags;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700423 int ret;
Stephen Boydfeac18d2011-11-07 19:48:26 -0800424 struct debug_obj o = { .object = addr,
425 .state = ODEBUG_STATE_NOTAVAILABLE,
426 .descr = descr };
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700427
428 if (!debug_objects_enabled)
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700429 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700430
431 db = get_bucket((unsigned long) addr);
432
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100433 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700434
435 obj = lookup_object(addr, db);
436 if (obj) {
437 switch (obj->state) {
438 case ODEBUG_STATE_INIT:
439 case ODEBUG_STATE_INACTIVE:
440 obj->state = ODEBUG_STATE_ACTIVE;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700441 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700442 break;
443
444 case ODEBUG_STATE_ACTIVE:
445 debug_print_object(obj, "activate");
446 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100447 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700448 ret = debug_object_fixup(descr->fixup_activate, addr, state);
Du, Changbine7a8e782016-05-19 17:09:23 -0700449 return ret ? 0 : -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700450
451 case ODEBUG_STATE_DESTROYED:
452 debug_print_object(obj, "activate");
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700453 ret = -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700454 break;
455 default:
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700456 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700457 break;
458 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100459 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700460 return ret;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700461 }
462
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100463 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700464 /*
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700465 * We are here when a static object is activated. We
466 * let the type specific code confirm whether this is
467 * true or not. if true, we just make sure that the
468 * static object is tracked in the object tracker. If
469 * not, this must be a bug, so we try to fix it up.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700470 */
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700471 if (descr->is_static_object && descr->is_static_object(addr)) {
472 /* track this static object */
473 debug_object_init(addr, descr);
474 debug_object_activate(addr, descr);
475 } else {
Stephen Boydfeac18d2011-11-07 19:48:26 -0800476 debug_print_object(&o, "activate");
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700477 ret = debug_object_fixup(descr->fixup_activate, addr,
478 ODEBUG_STATE_NOTAVAILABLE);
479 return ret ? 0 : -EINVAL;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700480 }
481 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700482}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800483EXPORT_SYMBOL_GPL(debug_object_activate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700484
485/**
486 * debug_object_deactivate - debug checks when an object is deactivated
487 * @addr: address of the object
488 * @descr: pointer to an object specific debug description structure
489 */
490void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
491{
492 struct debug_bucket *db;
493 struct debug_obj *obj;
494 unsigned long flags;
495
496 if (!debug_objects_enabled)
497 return;
498
499 db = get_bucket((unsigned long) addr);
500
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100501 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700502
503 obj = lookup_object(addr, db);
504 if (obj) {
505 switch (obj->state) {
506 case ODEBUG_STATE_INIT:
507 case ODEBUG_STATE_INACTIVE:
508 case ODEBUG_STATE_ACTIVE:
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400509 if (!obj->astate)
510 obj->state = ODEBUG_STATE_INACTIVE;
511 else
512 debug_print_object(obj, "deactivate");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700513 break;
514
515 case ODEBUG_STATE_DESTROYED:
516 debug_print_object(obj, "deactivate");
517 break;
518 default:
519 break;
520 }
521 } else {
522 struct debug_obj o = { .object = addr,
523 .state = ODEBUG_STATE_NOTAVAILABLE,
524 .descr = descr };
525
526 debug_print_object(&o, "deactivate");
527 }
528
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100529 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700530}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800531EXPORT_SYMBOL_GPL(debug_object_deactivate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700532
533/**
534 * debug_object_destroy - debug checks when an object is destroyed
535 * @addr: address of the object
536 * @descr: pointer to an object specific debug description structure
537 */
538void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
539{
540 enum debug_obj_state state;
541 struct debug_bucket *db;
542 struct debug_obj *obj;
543 unsigned long flags;
544
545 if (!debug_objects_enabled)
546 return;
547
548 db = get_bucket((unsigned long) addr);
549
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100550 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700551
552 obj = lookup_object(addr, db);
553 if (!obj)
554 goto out_unlock;
555
556 switch (obj->state) {
557 case ODEBUG_STATE_NONE:
558 case ODEBUG_STATE_INIT:
559 case ODEBUG_STATE_INACTIVE:
560 obj->state = ODEBUG_STATE_DESTROYED;
561 break;
562 case ODEBUG_STATE_ACTIVE:
563 debug_print_object(obj, "destroy");
564 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100565 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700566 debug_object_fixup(descr->fixup_destroy, addr, state);
567 return;
568
569 case ODEBUG_STATE_DESTROYED:
570 debug_print_object(obj, "destroy");
571 break;
572 default:
573 break;
574 }
575out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100576 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700577}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800578EXPORT_SYMBOL_GPL(debug_object_destroy);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700579
580/**
581 * debug_object_free - debug checks when an object is freed
582 * @addr: address of the object
583 * @descr: pointer to an object specific debug description structure
584 */
585void debug_object_free(void *addr, struct debug_obj_descr *descr)
586{
587 enum debug_obj_state state;
588 struct debug_bucket *db;
589 struct debug_obj *obj;
590 unsigned long flags;
591
592 if (!debug_objects_enabled)
593 return;
594
595 db = get_bucket((unsigned long) addr);
596
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100597 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700598
599 obj = lookup_object(addr, db);
600 if (!obj)
601 goto out_unlock;
602
603 switch (obj->state) {
604 case ODEBUG_STATE_ACTIVE:
605 debug_print_object(obj, "free");
606 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100607 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700608 debug_object_fixup(descr->fixup_free, addr, state);
609 return;
610 default:
611 hlist_del(&obj->node);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100612 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700613 free_object(obj);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200614 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700615 }
616out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100617 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700618}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800619EXPORT_SYMBOL_GPL(debug_object_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700620
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400621/**
Christine Chanb84d4352011-11-07 19:48:27 -0800622 * debug_object_assert_init - debug checks when object should be init-ed
623 * @addr: address of the object
624 * @descr: pointer to an object specific debug description structure
625 */
626void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
627{
628 struct debug_bucket *db;
629 struct debug_obj *obj;
630 unsigned long flags;
631
632 if (!debug_objects_enabled)
633 return;
634
635 db = get_bucket((unsigned long) addr);
636
637 raw_spin_lock_irqsave(&db->lock, flags);
638
639 obj = lookup_object(addr, db);
640 if (!obj) {
641 struct debug_obj o = { .object = addr,
642 .state = ODEBUG_STATE_NOTAVAILABLE,
643 .descr = descr };
644
645 raw_spin_unlock_irqrestore(&db->lock, flags);
646 /*
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700647 * Maybe the object is static, and we let the type specific
648 * code confirm. Track this static object if true, else invoke
649 * fixup.
Christine Chanb84d4352011-11-07 19:48:27 -0800650 */
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700651 if (descr->is_static_object && descr->is_static_object(addr)) {
652 /* Track this static object */
653 debug_object_init(addr, descr);
654 } else {
Christine Chanb84d4352011-11-07 19:48:27 -0800655 debug_print_object(&o, "assert_init");
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700656 debug_object_fixup(descr->fixup_assert_init, addr,
657 ODEBUG_STATE_NOTAVAILABLE);
658 }
Christine Chanb84d4352011-11-07 19:48:27 -0800659 return;
660 }
661
662 raw_spin_unlock_irqrestore(&db->lock, flags);
663}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800664EXPORT_SYMBOL_GPL(debug_object_assert_init);
Christine Chanb84d4352011-11-07 19:48:27 -0800665
666/**
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400667 * debug_object_active_state - debug checks object usage state machine
668 * @addr: address of the object
669 * @descr: pointer to an object specific debug description structure
670 * @expect: expected state
671 * @next: state to move to if expected state is found
672 */
673void
674debug_object_active_state(void *addr, struct debug_obj_descr *descr,
675 unsigned int expect, unsigned int next)
676{
677 struct debug_bucket *db;
678 struct debug_obj *obj;
679 unsigned long flags;
680
681 if (!debug_objects_enabled)
682 return;
683
684 db = get_bucket((unsigned long) addr);
685
686 raw_spin_lock_irqsave(&db->lock, flags);
687
688 obj = lookup_object(addr, db);
689 if (obj) {
690 switch (obj->state) {
691 case ODEBUG_STATE_ACTIVE:
692 if (obj->astate == expect)
693 obj->astate = next;
694 else
695 debug_print_object(obj, "active_state");
696 break;
697
698 default:
699 debug_print_object(obj, "active_state");
700 break;
701 }
702 } else {
703 struct debug_obj o = { .object = addr,
704 .state = ODEBUG_STATE_NOTAVAILABLE,
705 .descr = descr };
706
707 debug_print_object(&o, "active_state");
708 }
709
710 raw_spin_unlock_irqrestore(&db->lock, flags);
711}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800712EXPORT_SYMBOL_GPL(debug_object_active_state);
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400713
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700714#ifdef CONFIG_DEBUG_OBJECTS_FREE
715static void __debug_check_no_obj_freed(const void *address, unsigned long size)
716{
717 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800718 struct hlist_node *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200719 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700720 struct debug_obj_descr *descr;
721 enum debug_obj_state state;
722 struct debug_bucket *db;
723 struct debug_obj *obj;
Yang Shibd9dcd02018-02-06 07:18:25 +0800724 int cnt, objs_checked = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700725
726 saddr = (unsigned long) address;
727 eaddr = saddr + size;
728 paddr = saddr & ODEBUG_CHUNK_MASK;
729 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
730 chunks >>= ODEBUG_CHUNK_SHIFT;
731
732 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
733 db = get_bucket(paddr);
734
735repeat:
736 cnt = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100737 raw_spin_lock_irqsave(&db->lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800738 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700739 cnt++;
740 oaddr = (unsigned long) obj->object;
741 if (oaddr < saddr || oaddr >= eaddr)
742 continue;
743
744 switch (obj->state) {
745 case ODEBUG_STATE_ACTIVE:
746 debug_print_object(obj, "free");
747 descr = obj->descr;
748 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100749 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700750 debug_object_fixup(descr->fixup_free,
751 (void *) oaddr, state);
752 goto repeat;
753 default:
754 hlist_del(&obj->node);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200755 hlist_add_head(&obj->node, &freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700756 break;
757 }
758 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100759 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200760
761 /* Now free them */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800762 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200763 hlist_del(&obj->node);
764 free_object(obj);
765 }
766
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700767 if (cnt > debug_objects_maxchain)
768 debug_objects_maxchain = cnt;
Yang Shibd9dcd02018-02-06 07:18:25 +0800769
770 objs_checked += cnt;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700771 }
Yang Shibd9dcd02018-02-06 07:18:25 +0800772
773 if (objs_checked > debug_objects_maxchecked)
774 debug_objects_maxchecked = objs_checked;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700775}
776
777void debug_check_no_obj_freed(const void *address, unsigned long size)
778{
779 if (debug_objects_enabled)
780 __debug_check_no_obj_freed(address, size);
781}
782#endif
783
784#ifdef CONFIG_DEBUG_FS
785
786static int debug_stats_show(struct seq_file *m, void *v)
787{
788 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
Yang Shibd9dcd02018-02-06 07:18:25 +0800789 seq_printf(m, "max_checked :%d\n", debug_objects_maxchecked);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700790 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
791 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
792 seq_printf(m, "pool_free :%d\n", obj_pool_free);
793 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
794 seq_printf(m, "pool_used :%d\n", obj_pool_used);
795 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
Waiman Long0cad93c2017-02-07 16:40:30 -0500796 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
797 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700798 return 0;
799}
800
801static int debug_stats_open(struct inode *inode, struct file *filp)
802{
803 return single_open(filp, debug_stats_show, NULL);
804}
805
806static const struct file_operations debug_stats_fops = {
807 .open = debug_stats_open,
808 .read = seq_read,
809 .llseek = seq_lseek,
810 .release = single_release,
811};
812
813static int __init debug_objects_init_debugfs(void)
814{
815 struct dentry *dbgdir, *dbgstats;
816
817 if (!debug_objects_enabled)
818 return 0;
819
820 dbgdir = debugfs_create_dir("debug_objects", NULL);
821 if (!dbgdir)
822 return -ENOMEM;
823
824 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
825 &debug_stats_fops);
826 if (!dbgstats)
827 goto err;
828
829 return 0;
830
831err:
832 debugfs_remove(dbgdir);
833
834 return -ENOMEM;
835}
836__initcall(debug_objects_init_debugfs);
837
838#else
839static inline void debug_objects_init_debugfs(void) { }
840#endif
841
842#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
843
844/* Random data structure for the self test */
845struct self_test {
846 unsigned long dummy1[6];
847 int static_init;
848 unsigned long dummy2[3];
849};
850
851static __initdata struct debug_obj_descr descr_type_test;
852
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700853static bool __init is_static_object(void *addr)
854{
855 struct self_test *obj = addr;
856
857 return obj->static_init;
858}
859
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700860/*
861 * fixup_init is called when:
862 * - an active object is initialized
863 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700864static bool __init fixup_init(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700865{
866 struct self_test *obj = addr;
867
868 switch (state) {
869 case ODEBUG_STATE_ACTIVE:
870 debug_object_deactivate(obj, &descr_type_test);
871 debug_object_init(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700872 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700873 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700874 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700875 }
876}
877
878/*
879 * fixup_activate is called when:
880 * - an active object is activated
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700881 * - an unknown non-static object is activated
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700882 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700883static bool __init fixup_activate(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700884{
885 struct self_test *obj = addr;
886
887 switch (state) {
888 case ODEBUG_STATE_NOTAVAILABLE:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700889 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700890 case ODEBUG_STATE_ACTIVE:
891 debug_object_deactivate(obj, &descr_type_test);
892 debug_object_activate(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700893 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700894
895 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700896 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700897 }
898}
899
900/*
901 * fixup_destroy is called when:
902 * - an active object is destroyed
903 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700904static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700905{
906 struct self_test *obj = addr;
907
908 switch (state) {
909 case ODEBUG_STATE_ACTIVE:
910 debug_object_deactivate(obj, &descr_type_test);
911 debug_object_destroy(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700912 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700913 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700914 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700915 }
916}
917
918/*
919 * fixup_free is called when:
920 * - an active object is freed
921 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700922static bool __init fixup_free(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700923{
924 struct self_test *obj = addr;
925
926 switch (state) {
927 case ODEBUG_STATE_ACTIVE:
928 debug_object_deactivate(obj, &descr_type_test);
929 debug_object_free(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700930 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700931 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700932 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700933 }
934}
935
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +0100936static int __init
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700937check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
938{
939 struct debug_bucket *db;
940 struct debug_obj *obj;
941 unsigned long flags;
942 int res = -EINVAL;
943
944 db = get_bucket((unsigned long) addr);
945
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100946 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700947
948 obj = lookup_object(addr, db);
949 if (!obj && state != ODEBUG_STATE_NONE) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700950 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700951 goto out;
952 }
953 if (obj && obj->state != state) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700954 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700955 obj->state, state);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700956 goto out;
957 }
958 if (fixups != debug_objects_fixups) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700959 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700960 fixups, debug_objects_fixups);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700961 goto out;
962 }
963 if (warnings != debug_objects_warnings) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700964 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700965 warnings, debug_objects_warnings);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700966 goto out;
967 }
968 res = 0;
969out:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100970 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700971 if (res)
972 debug_objects_enabled = 0;
973 return res;
974}
975
976static __initdata struct debug_obj_descr descr_type_test = {
977 .name = "selftest",
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700978 .is_static_object = is_static_object,
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700979 .fixup_init = fixup_init,
980 .fixup_activate = fixup_activate,
981 .fixup_destroy = fixup_destroy,
982 .fixup_free = fixup_free,
983};
984
985static __initdata struct self_test obj = { .static_init = 0 };
986
987static void __init debug_objects_selftest(void)
988{
989 int fixups, oldfixups, warnings, oldwarnings;
990 unsigned long flags;
991
992 local_irq_save(flags);
993
994 fixups = oldfixups = debug_objects_fixups;
995 warnings = oldwarnings = debug_objects_warnings;
996 descr_test = &descr_type_test;
997
998 debug_object_init(&obj, &descr_type_test);
999 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1000 goto out;
1001 debug_object_activate(&obj, &descr_type_test);
1002 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1003 goto out;
1004 debug_object_activate(&obj, &descr_type_test);
1005 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1006 goto out;
1007 debug_object_deactivate(&obj, &descr_type_test);
1008 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1009 goto out;
1010 debug_object_destroy(&obj, &descr_type_test);
1011 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1012 goto out;
1013 debug_object_init(&obj, &descr_type_test);
1014 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1015 goto out;
1016 debug_object_activate(&obj, &descr_type_test);
1017 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1018 goto out;
1019 debug_object_deactivate(&obj, &descr_type_test);
1020 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1021 goto out;
1022 debug_object_free(&obj, &descr_type_test);
1023 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1024 goto out;
1025
1026 obj.static_init = 1;
1027 debug_object_activate(&obj, &descr_type_test);
Stephen Boyd9f78ff02012-03-05 14:59:17 -08001028 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001029 goto out;
1030 debug_object_init(&obj, &descr_type_test);
1031 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1032 goto out;
1033 debug_object_free(&obj, &descr_type_test);
1034 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1035 goto out;
1036
1037#ifdef CONFIG_DEBUG_OBJECTS_FREE
1038 debug_object_init(&obj, &descr_type_test);
1039 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1040 goto out;
1041 debug_object_activate(&obj, &descr_type_test);
1042 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1043 goto out;
1044 __debug_check_no_obj_freed(&obj, sizeof(obj));
1045 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1046 goto out;
1047#endif
Fabian Frederick719e4842014-06-04 16:06:04 -07001048 pr_info("selftest passed\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001049
1050out:
1051 debug_objects_fixups = oldfixups;
1052 debug_objects_warnings = oldwarnings;
1053 descr_test = NULL;
1054
1055 local_irq_restore(flags);
1056}
1057#else
1058static inline void debug_objects_selftest(void) { }
1059#endif
1060
1061/*
1062 * Called during early boot to initialize the hash buckets and link
1063 * the static object pool objects into the poll list. After this call
1064 * the object tracker is fully operational.
1065 */
1066void __init debug_objects_early_init(void)
1067{
1068 int i;
1069
1070 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001071 raw_spin_lock_init(&obj_hash[i].lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001072
1073 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1074 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1075}
1076
1077/*
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001078 * Convert the statically allocated objects to dynamic ones:
1079 */
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001080static int __init debug_objects_replace_static_objects(void)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001081{
1082 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001083 struct hlist_node *tmp;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001084 struct debug_obj *obj, *new;
1085 HLIST_HEAD(objects);
1086 int i, cnt = 0;
1087
1088 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1089 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1090 if (!obj)
1091 goto free;
Waiman Longcaba4cb2017-08-14 09:52:13 -04001092 kmemleak_ignore(obj);
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001093 hlist_add_head(&obj->node, &objects);
1094 }
1095
1096 /*
1097 * When debug_objects_mem_init() is called we know that only
1098 * one CPU is up, so disabling interrupts is enough
1099 * protection. This avoids the lockdep hell of lock ordering.
1100 */
1101 local_irq_disable();
1102
1103 /* Remove the statically allocated objects from the pool */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001104 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001105 hlist_del(&obj->node);
1106 /* Move the allocated objects to the pool */
1107 hlist_move_list(&objects, &obj_pool);
1108
1109 /* Replace the active object references */
1110 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1111 hlist_move_list(&db->list, &objects);
1112
Sasha Levinb67bfe02013-02-27 17:06:00 -08001113 hlist_for_each_entry(obj, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001114 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1115 hlist_del(&new->node);
1116 /* copy object data */
1117 *new = *obj;
1118 hlist_add_head(&new->node, &db->list);
1119 cnt++;
1120 }
1121 }
Thomas Gleixner765a5e02012-04-11 11:54:27 +02001122 local_irq_enable();
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001123
Fabian Frederickc0f35cc2014-06-04 16:06:05 -07001124 pr_debug("%d of %d active objects replaced\n",
1125 cnt, obj_pool_used);
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001126 return 0;
1127free:
Sasha Levinb67bfe02013-02-27 17:06:00 -08001128 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001129 hlist_del(&obj->node);
1130 kmem_cache_free(obj_cache, obj);
1131 }
1132 return -ENOMEM;
1133}
1134
1135/*
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001136 * Called after the kmem_caches are functional to setup a dedicated
1137 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1138 * prevents that the debug code is called on kmem_cache_free() for the
1139 * debug tracker objects to avoid recursive calls.
1140 */
1141void __init debug_objects_mem_init(void)
1142{
1143 if (!debug_objects_enabled)
1144 return;
1145
1146 obj_cache = kmem_cache_create("debug_objects_cache",
1147 sizeof (struct debug_obj), 0,
1148 SLAB_DEBUG_OBJECTS, NULL);
1149
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001150 if (!obj_cache || debug_objects_replace_static_objects()) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001151 debug_objects_enabled = 0;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001152 if (obj_cache)
1153 kmem_cache_destroy(obj_cache);
Fabian Frederick719e4842014-06-04 16:06:04 -07001154 pr_warn("out of memory.\n");
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001155 } else
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001156 debug_objects_selftest();
Waiman Long97dd5522017-01-05 15:17:04 -05001157
1158 /*
1159 * Increase the thresholds for allocating and freeing objects
1160 * according to the number of possible CPUs available in the system.
1161 */
1162 debug_objects_pool_size += num_possible_cpus() * 32;
1163 debug_objects_pool_min_level += num_possible_cpus() * 4;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001164}