]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - lib/debugobjects.c
locking/lockdep: Fix debug_locks off performance problem
[mirror_ubuntu-bionic-kernel.git] / lib / debugobjects.c
CommitLineData
3ac7fe5a
TG
1/*
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 */
719e4843
FF
10
11#define pr_fmt(fmt) "ODEBUG: " fmt
12
3ac7fe5a
TG
13#include <linux/debugobjects.h>
14#include <linux/interrupt.h>
d43c36dc 15#include <linux/sched.h>
68db0cf1 16#include <linux/sched/task_stack.h>
3ac7fe5a
TG
17#include <linux/seq_file.h>
18#include <linux/debugfs.h>
5a0e3ad6 19#include <linux/slab.h>
3ac7fe5a 20#include <linux/hash.h>
caba4cbb 21#include <linux/kmemleak.h>
3ac7fe5a
TG
22
23#define ODEBUG_HASH_BITS 14
24#define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
25
0b6ec8c0 26#define ODEBUG_POOL_SIZE 1024
3ac7fe5a
TG
27#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;
aef9cb05 35 raw_spinlock_t lock;
3ac7fe5a
TG
36};
37
38static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
39
1be1cb7b 40static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
3ac7fe5a 41
aef9cb05 42static DEFINE_RAW_SPINLOCK(pool_lock);
3ac7fe5a
TG
43
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;
53static int debug_objects_fixups __read_mostly;
54static int debug_objects_warnings __read_mostly;
3ae70205
IM
55static int debug_objects_enabled __read_mostly
56 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
97dd552e
WL
57static int debug_objects_pool_size __read_mostly
58 = ODEBUG_POOL_SIZE;
59static int debug_objects_pool_min_level __read_mostly
60 = ODEBUG_POOL_MIN_LEVEL;
3ac7fe5a
TG
61static struct debug_obj_descr *descr_test __read_mostly;
62
c4b73aab 63/*
0cad93c3 64 * Track numbers of kmem_cache_alloc()/free() calls done.
c4b73aab 65 */
0cad93c3 66static int debug_objects_allocated;
c4b73aab
WL
67static int debug_objects_freed;
68
337fff8b
TG
69static void free_obj_work(struct work_struct *work);
70static DECLARE_WORK(debug_obj_work, free_obj_work);
71
3ac7fe5a
TG
72static int __init enable_object_debug(char *str)
73{
74 debug_objects_enabled = 1;
75 return 0;
76}
3e8ebb5c
KM
77
78static int __init disable_object_debug(char *str)
79{
80 debug_objects_enabled = 0;
81 return 0;
82}
83
3ac7fe5a 84early_param("debug_objects", enable_object_debug);
3e8ebb5c 85early_param("no_debug_objects", disable_object_debug);
3ac7fe5a
TG
86
87static const char *obj_states[ODEBUG_STATE_MAX] = {
88 [ODEBUG_STATE_NONE] = "none",
89 [ODEBUG_STATE_INIT] = "initialized",
90 [ODEBUG_STATE_INACTIVE] = "inactive",
91 [ODEBUG_STATE_ACTIVE] = "active",
92 [ODEBUG_STATE_DESTROYED] = "destroyed",
93 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
94};
95
1fda107d 96static void fill_pool(void)
3ac7fe5a
TG
97{
98 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
99 struct debug_obj *new;
50db04dd 100 unsigned long flags;
3ac7fe5a 101
97dd552e 102 if (likely(obj_pool_free >= debug_objects_pool_min_level))
1fda107d 103 return;
3ac7fe5a
TG
104
105 if (unlikely(!obj_cache))
1fda107d 106 return;
3ac7fe5a 107
97dd552e 108 while (obj_pool_free < debug_objects_pool_min_level) {
3ac7fe5a
TG
109
110 new = kmem_cache_zalloc(obj_cache, gfp);
111 if (!new)
3340808c 112 return;
3ac7fe5a 113
caba4cbb 114 kmemleak_ignore(new);
aef9cb05 115 raw_spin_lock_irqsave(&pool_lock, flags);
3ac7fe5a 116 hlist_add_head(&new->node, &obj_pool);
0cad93c3 117 debug_objects_allocated++;
3ac7fe5a 118 obj_pool_free++;
aef9cb05 119 raw_spin_unlock_irqrestore(&pool_lock, flags);
3ac7fe5a 120 }
3ac7fe5a
TG
121}
122
123/*
124 * Lookup an object in the hash bucket.
125 */
126static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
127{
3ac7fe5a
TG
128 struct debug_obj *obj;
129 int cnt = 0;
130
b67bfe0d 131 hlist_for_each_entry(obj, &b->list, node) {
3ac7fe5a
TG
132 cnt++;
133 if (obj->object == addr)
134 return obj;
135 }
136 if (cnt > debug_objects_maxchain)
137 debug_objects_maxchain = cnt;
138
139 return NULL;
140}
141
142/*
50db04dd 143 * Allocate a new object. If the pool is empty, switch off the debugger.
673d62cc 144 * Must be called with interrupts disabled.
3ac7fe5a
TG
145 */
146static struct debug_obj *
147alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
148{
149 struct debug_obj *obj = NULL;
3ac7fe5a 150
aef9cb05 151 raw_spin_lock(&pool_lock);
3ac7fe5a
TG
152 if (obj_pool.first) {
153 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
154
155 obj->object = addr;
156 obj->descr = descr;
157 obj->state = ODEBUG_STATE_NONE;
a5d8e467 158 obj->astate = 0;
3ac7fe5a
TG
159 hlist_del(&obj->node);
160
161 hlist_add_head(&obj->node, &b->list);
162
163 obj_pool_used++;
164 if (obj_pool_used > obj_pool_max_used)
165 obj_pool_max_used = obj_pool_used;
166
167 obj_pool_free--;
168 if (obj_pool_free < obj_pool_min_free)
169 obj_pool_min_free = obj_pool_free;
170 }
aef9cb05 171 raw_spin_unlock(&pool_lock);
3ac7fe5a 172
3ac7fe5a
TG
173 return obj;
174}
175
176/*
337fff8b 177 * workqueue function to free objects.
858274b6
WL
178 *
179 * To reduce contention on the global pool_lock, the actual freeing of
180 * debug objects will be delayed if the pool_lock is busy. We also free
181 * the objects in a batch of 4 for each lock/unlock cycle.
3ac7fe5a 182 */
858274b6
WL
183#define ODEBUG_FREE_BATCH 4
184
337fff8b 185static void free_obj_work(struct work_struct *work)
3ac7fe5a 186{
858274b6 187 struct debug_obj *objs[ODEBUG_FREE_BATCH];
673d62cc 188 unsigned long flags;
858274b6 189 int i;
3ac7fe5a 190
858274b6
WL
191 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
192 return;
193 while (obj_pool_free >= debug_objects_pool_size + ODEBUG_FREE_BATCH) {
194 for (i = 0; i < ODEBUG_FREE_BATCH; i++) {
195 objs[i] = hlist_entry(obj_pool.first,
196 typeof(*objs[0]), node);
197 hlist_del(&objs[i]->node);
198 }
199
200 obj_pool_free -= ODEBUG_FREE_BATCH;
201 debug_objects_freed += ODEBUG_FREE_BATCH;
337fff8b
TG
202 /*
203 * We release pool_lock across kmem_cache_free() to
204 * avoid contention on pool_lock.
205 */
aef9cb05 206 raw_spin_unlock_irqrestore(&pool_lock, flags);
858274b6
WL
207 for (i = 0; i < ODEBUG_FREE_BATCH; i++)
208 kmem_cache_free(obj_cache, objs[i]);
209 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
210 return;
3ac7fe5a 211 }
aef9cb05 212 raw_spin_unlock_irqrestore(&pool_lock, flags);
337fff8b
TG
213}
214
215/*
216 * Put the object back into the pool and schedule work to free objects
217 * if necessary.
218 */
219static void free_object(struct debug_obj *obj)
220{
221 unsigned long flags;
222 int sched = 0;
223
aef9cb05 224 raw_spin_lock_irqsave(&pool_lock, flags);
337fff8b
TG
225 /*
226 * schedule work when the pool is filled and the cache is
227 * initialized:
228 */
97dd552e 229 if (obj_pool_free > debug_objects_pool_size && obj_cache)
7092dff2 230 sched = 1;
337fff8b
TG
231 hlist_add_head(&obj->node, &obj_pool);
232 obj_pool_free++;
233 obj_pool_used--;
aef9cb05 234 raw_spin_unlock_irqrestore(&pool_lock, flags);
337fff8b
TG
235 if (sched)
236 schedule_work(&debug_obj_work);
3ac7fe5a
TG
237}
238
239/*
240 * We run out of memory. That means we probably have tons of objects
241 * allocated.
242 */
243static void debug_objects_oom(void)
244{
245 struct debug_bucket *db = obj_hash;
b67bfe0d 246 struct hlist_node *tmp;
673d62cc 247 HLIST_HEAD(freelist);
3ac7fe5a
TG
248 struct debug_obj *obj;
249 unsigned long flags;
250 int i;
251
719e4843 252 pr_warn("Out of memory. ODEBUG disabled\n");
3ac7fe5a
TG
253
254 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
aef9cb05 255 raw_spin_lock_irqsave(&db->lock, flags);
673d62cc 256 hlist_move_list(&db->list, &freelist);
aef9cb05 257 raw_spin_unlock_irqrestore(&db->lock, flags);
673d62cc
VN
258
259 /* Now free them */
b67bfe0d 260 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
3ac7fe5a
TG
261 hlist_del(&obj->node);
262 free_object(obj);
263 }
3ac7fe5a
TG
264 }
265}
266
267/*
268 * We use the pfn of the address for the hash. That way we can check
269 * for freed objects simply by checking the affected bucket.
270 */
271static struct debug_bucket *get_bucket(unsigned long addr)
272{
273 unsigned long hash;
274
275 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
276 return &obj_hash[hash];
277}
278
279static void debug_print_object(struct debug_obj *obj, char *msg)
280{
99777288 281 struct debug_obj_descr *descr = obj->descr;
3ac7fe5a
TG
282 static int limit;
283
99777288
SG
284 if (limit < 5 && descr != descr_test) {
285 void *hint = descr->debug_hint ?
286 descr->debug_hint(obj->object) : NULL;
3ac7fe5a 287 limit++;
a5d8e467 288 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
99777288 289 "object type: %s hint: %pS\n",
a5d8e467 290 msg, obj_states[obj->state], obj->astate,
99777288 291 descr->name, hint);
3ac7fe5a
TG
292 }
293 debug_objects_warnings++;
294}
295
296/*
297 * Try to repair the damage, so we have a better chance to get useful
298 * debug output.
299 */
b1e4d9d8
DC
300static bool
301debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
3ac7fe5a
TG
302 void * addr, enum debug_obj_state state)
303{
b1e4d9d8
DC
304 if (fixup && fixup(addr, state)) {
305 debug_objects_fixups++;
306 return true;
307 }
308 return false;
3ac7fe5a
TG
309}
310
311static void debug_object_is_on_stack(void *addr, int onstack)
312{
3ac7fe5a
TG
313 int is_on_stack;
314 static int limit;
315
316 if (limit > 4)
317 return;
318
8b05c7e6 319 is_on_stack = object_is_on_stack(addr);
3ac7fe5a
TG
320 if (is_on_stack == onstack)
321 return;
322
323 limit++;
324 if (is_on_stack)
f9c10413
JFG
325 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
326 task_stack_page(current));
3ac7fe5a 327 else
f9c10413
JFG
328 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
329 task_stack_page(current));
330
3ac7fe5a
TG
331 WARN_ON(1);
332}
333
334static void
335__debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
336{
337 enum debug_obj_state state;
338 struct debug_bucket *db;
339 struct debug_obj *obj;
340 unsigned long flags;
341
50db04dd
VN
342 fill_pool();
343
3ac7fe5a
TG
344 db = get_bucket((unsigned long) addr);
345
aef9cb05 346 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
347
348 obj = lookup_object(addr, db);
349 if (!obj) {
350 obj = alloc_object(addr, db, descr);
351 if (!obj) {
352 debug_objects_enabled = 0;
aef9cb05 353 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
354 debug_objects_oom();
355 return;
356 }
357 debug_object_is_on_stack(addr, onstack);
358 }
359
360 switch (obj->state) {
361 case ODEBUG_STATE_NONE:
362 case ODEBUG_STATE_INIT:
363 case ODEBUG_STATE_INACTIVE:
364 obj->state = ODEBUG_STATE_INIT;
365 break;
366
367 case ODEBUG_STATE_ACTIVE:
368 debug_print_object(obj, "init");
369 state = obj->state;
aef9cb05 370 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
371 debug_object_fixup(descr->fixup_init, addr, state);
372 return;
373
374 case ODEBUG_STATE_DESTROYED:
375 debug_print_object(obj, "init");
376 break;
377 default:
378 break;
379 }
380
aef9cb05 381 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
382}
383
384/**
385 * debug_object_init - debug checks when an object is initialized
386 * @addr: address of the object
387 * @descr: pointer to an object specific debug description structure
388 */
389void debug_object_init(void *addr, struct debug_obj_descr *descr)
390{
391 if (!debug_objects_enabled)
392 return;
393
394 __debug_object_init(addr, descr, 0);
395}
f8ff04e2 396EXPORT_SYMBOL_GPL(debug_object_init);
3ac7fe5a
TG
397
398/**
399 * debug_object_init_on_stack - debug checks when an object on stack is
400 * initialized
401 * @addr: address of the object
402 * @descr: pointer to an object specific debug description structure
403 */
404void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
405{
406 if (!debug_objects_enabled)
407 return;
408
409 __debug_object_init(addr, descr, 1);
410}
f8ff04e2 411EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
3ac7fe5a
TG
412
413/**
414 * debug_object_activate - debug checks when an object is activated
415 * @addr: address of the object
416 * @descr: pointer to an object specific debug description structure
b778ae25 417 * Returns 0 for success, -EINVAL for check failed.
3ac7fe5a 418 */
b778ae25 419int debug_object_activate(void *addr, struct debug_obj_descr *descr)
3ac7fe5a
TG
420{
421 enum debug_obj_state state;
422 struct debug_bucket *db;
423 struct debug_obj *obj;
424 unsigned long flags;
b778ae25 425 int ret;
feac18dd
SB
426 struct debug_obj o = { .object = addr,
427 .state = ODEBUG_STATE_NOTAVAILABLE,
428 .descr = descr };
3ac7fe5a
TG
429
430 if (!debug_objects_enabled)
b778ae25 431 return 0;
3ac7fe5a
TG
432
433 db = get_bucket((unsigned long) addr);
434
aef9cb05 435 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
436
437 obj = lookup_object(addr, db);
438 if (obj) {
439 switch (obj->state) {
440 case ODEBUG_STATE_INIT:
441 case ODEBUG_STATE_INACTIVE:
442 obj->state = ODEBUG_STATE_ACTIVE;
b778ae25 443 ret = 0;
3ac7fe5a
TG
444 break;
445
446 case ODEBUG_STATE_ACTIVE:
447 debug_print_object(obj, "activate");
448 state = obj->state;
aef9cb05 449 raw_spin_unlock_irqrestore(&db->lock, flags);
b778ae25 450 ret = debug_object_fixup(descr->fixup_activate, addr, state);
e7a8e78b 451 return ret ? 0 : -EINVAL;
3ac7fe5a
TG
452
453 case ODEBUG_STATE_DESTROYED:
454 debug_print_object(obj, "activate");
b778ae25 455 ret = -EINVAL;
3ac7fe5a
TG
456 break;
457 default:
b778ae25 458 ret = 0;
3ac7fe5a
TG
459 break;
460 }
aef9cb05 461 raw_spin_unlock_irqrestore(&db->lock, flags);
b778ae25 462 return ret;
3ac7fe5a
TG
463 }
464
aef9cb05 465 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a 466 /*
b9fdac7f
DC
467 * We are here when a static object is activated. We
468 * let the type specific code confirm whether this is
469 * true or not. if true, we just make sure that the
470 * static object is tracked in the object tracker. If
471 * not, this must be a bug, so we try to fix it up.
3ac7fe5a 472 */
b9fdac7f
DC
473 if (descr->is_static_object && descr->is_static_object(addr)) {
474 /* track this static object */
475 debug_object_init(addr, descr);
476 debug_object_activate(addr, descr);
477 } else {
feac18dd 478 debug_print_object(&o, "activate");
b9fdac7f
DC
479 ret = debug_object_fixup(descr->fixup_activate, addr,
480 ODEBUG_STATE_NOTAVAILABLE);
481 return ret ? 0 : -EINVAL;
b778ae25
PM
482 }
483 return 0;
3ac7fe5a 484}
f8ff04e2 485EXPORT_SYMBOL_GPL(debug_object_activate);
3ac7fe5a
TG
486
487/**
488 * debug_object_deactivate - debug checks when an object is deactivated
489 * @addr: address of the object
490 * @descr: pointer to an object specific debug description structure
491 */
492void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
493{
494 struct debug_bucket *db;
495 struct debug_obj *obj;
496 unsigned long flags;
497
498 if (!debug_objects_enabled)
499 return;
500
501 db = get_bucket((unsigned long) addr);
502
aef9cb05 503 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
504
505 obj = lookup_object(addr, db);
506 if (obj) {
507 switch (obj->state) {
508 case ODEBUG_STATE_INIT:
509 case ODEBUG_STATE_INACTIVE:
510 case ODEBUG_STATE_ACTIVE:
a5d8e467
MD
511 if (!obj->astate)
512 obj->state = ODEBUG_STATE_INACTIVE;
513 else
514 debug_print_object(obj, "deactivate");
3ac7fe5a
TG
515 break;
516
517 case ODEBUG_STATE_DESTROYED:
518 debug_print_object(obj, "deactivate");
519 break;
520 default:
521 break;
522 }
523 } else {
524 struct debug_obj o = { .object = addr,
525 .state = ODEBUG_STATE_NOTAVAILABLE,
526 .descr = descr };
527
528 debug_print_object(&o, "deactivate");
529 }
530
aef9cb05 531 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a 532}
f8ff04e2 533EXPORT_SYMBOL_GPL(debug_object_deactivate);
3ac7fe5a
TG
534
535/**
536 * debug_object_destroy - debug checks when an object is destroyed
537 * @addr: address of the object
538 * @descr: pointer to an object specific debug description structure
539 */
540void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
541{
542 enum debug_obj_state state;
543 struct debug_bucket *db;
544 struct debug_obj *obj;
545 unsigned long flags;
546
547 if (!debug_objects_enabled)
548 return;
549
550 db = get_bucket((unsigned long) addr);
551
aef9cb05 552 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
553
554 obj = lookup_object(addr, db);
555 if (!obj)
556 goto out_unlock;
557
558 switch (obj->state) {
559 case ODEBUG_STATE_NONE:
560 case ODEBUG_STATE_INIT:
561 case ODEBUG_STATE_INACTIVE:
562 obj->state = ODEBUG_STATE_DESTROYED;
563 break;
564 case ODEBUG_STATE_ACTIVE:
565 debug_print_object(obj, "destroy");
566 state = obj->state;
aef9cb05 567 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
568 debug_object_fixup(descr->fixup_destroy, addr, state);
569 return;
570
571 case ODEBUG_STATE_DESTROYED:
572 debug_print_object(obj, "destroy");
573 break;
574 default:
575 break;
576 }
577out_unlock:
aef9cb05 578 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a 579}
f8ff04e2 580EXPORT_SYMBOL_GPL(debug_object_destroy);
3ac7fe5a
TG
581
582/**
583 * debug_object_free - debug checks when an object is freed
584 * @addr: address of the object
585 * @descr: pointer to an object specific debug description structure
586 */
587void debug_object_free(void *addr, struct debug_obj_descr *descr)
588{
589 enum debug_obj_state state;
590 struct debug_bucket *db;
591 struct debug_obj *obj;
592 unsigned long flags;
593
594 if (!debug_objects_enabled)
595 return;
596
597 db = get_bucket((unsigned long) addr);
598
aef9cb05 599 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
600
601 obj = lookup_object(addr, db);
602 if (!obj)
603 goto out_unlock;
604
605 switch (obj->state) {
606 case ODEBUG_STATE_ACTIVE:
607 debug_print_object(obj, "free");
608 state = obj->state;
aef9cb05 609 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
610 debug_object_fixup(descr->fixup_free, addr, state);
611 return;
612 default:
613 hlist_del(&obj->node);
aef9cb05 614 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a 615 free_object(obj);
673d62cc 616 return;
3ac7fe5a
TG
617 }
618out_unlock:
aef9cb05 619 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a 620}
f8ff04e2 621EXPORT_SYMBOL_GPL(debug_object_free);
3ac7fe5a 622
b84d435c
CC
623/**
624 * debug_object_assert_init - debug checks when object should be init-ed
625 * @addr: address of the object
626 * @descr: pointer to an object specific debug description structure
627 */
628void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
629{
630 struct debug_bucket *db;
631 struct debug_obj *obj;
632 unsigned long flags;
633
634 if (!debug_objects_enabled)
635 return;
636
637 db = get_bucket((unsigned long) addr);
638
639 raw_spin_lock_irqsave(&db->lock, flags);
640
641 obj = lookup_object(addr, db);
642 if (!obj) {
643 struct debug_obj o = { .object = addr,
644 .state = ODEBUG_STATE_NOTAVAILABLE,
645 .descr = descr };
646
647 raw_spin_unlock_irqrestore(&db->lock, flags);
648 /*
b9fdac7f
DC
649 * Maybe the object is static, and we let the type specific
650 * code confirm. Track this static object if true, else invoke
651 * fixup.
b84d435c 652 */
b9fdac7f
DC
653 if (descr->is_static_object && descr->is_static_object(addr)) {
654 /* Track this static object */
655 debug_object_init(addr, descr);
656 } else {
b84d435c 657 debug_print_object(&o, "assert_init");
b9fdac7f
DC
658 debug_object_fixup(descr->fixup_assert_init, addr,
659 ODEBUG_STATE_NOTAVAILABLE);
660 }
b84d435c
CC
661 return;
662 }
663
664 raw_spin_unlock_irqrestore(&db->lock, flags);
665}
f8ff04e2 666EXPORT_SYMBOL_GPL(debug_object_assert_init);
b84d435c 667
a5d8e467
MD
668/**
669 * debug_object_active_state - debug checks object usage state machine
670 * @addr: address of the object
671 * @descr: pointer to an object specific debug description structure
672 * @expect: expected state
673 * @next: state to move to if expected state is found
674 */
675void
676debug_object_active_state(void *addr, struct debug_obj_descr *descr,
677 unsigned int expect, unsigned int next)
678{
679 struct debug_bucket *db;
680 struct debug_obj *obj;
681 unsigned long flags;
682
683 if (!debug_objects_enabled)
684 return;
685
686 db = get_bucket((unsigned long) addr);
687
688 raw_spin_lock_irqsave(&db->lock, flags);
689
690 obj = lookup_object(addr, db);
691 if (obj) {
692 switch (obj->state) {
693 case ODEBUG_STATE_ACTIVE:
694 if (obj->astate == expect)
695 obj->astate = next;
696 else
697 debug_print_object(obj, "active_state");
698 break;
699
700 default:
701 debug_print_object(obj, "active_state");
702 break;
703 }
704 } else {
705 struct debug_obj o = { .object = addr,
706 .state = ODEBUG_STATE_NOTAVAILABLE,
707 .descr = descr };
708
709 debug_print_object(&o, "active_state");
710 }
711
712 raw_spin_unlock_irqrestore(&db->lock, flags);
713}
f8ff04e2 714EXPORT_SYMBOL_GPL(debug_object_active_state);
a5d8e467 715
3ac7fe5a
TG
716#ifdef CONFIG_DEBUG_OBJECTS_FREE
717static void __debug_check_no_obj_freed(const void *address, unsigned long size)
718{
719 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
b67bfe0d 720 struct hlist_node *tmp;
673d62cc 721 HLIST_HEAD(freelist);
3ac7fe5a
TG
722 struct debug_obj_descr *descr;
723 enum debug_obj_state state;
724 struct debug_bucket *db;
725 struct debug_obj *obj;
726 int cnt;
727
728 saddr = (unsigned long) address;
729 eaddr = saddr + size;
730 paddr = saddr & ODEBUG_CHUNK_MASK;
731 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
732 chunks >>= ODEBUG_CHUNK_SHIFT;
733
734 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
735 db = get_bucket(paddr);
736
737repeat:
738 cnt = 0;
aef9cb05 739 raw_spin_lock_irqsave(&db->lock, flags);
b67bfe0d 740 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
3ac7fe5a
TG
741 cnt++;
742 oaddr = (unsigned long) obj->object;
743 if (oaddr < saddr || oaddr >= eaddr)
744 continue;
745
746 switch (obj->state) {
747 case ODEBUG_STATE_ACTIVE:
748 debug_print_object(obj, "free");
749 descr = obj->descr;
750 state = obj->state;
aef9cb05 751 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
752 debug_object_fixup(descr->fixup_free,
753 (void *) oaddr, state);
754 goto repeat;
755 default:
756 hlist_del(&obj->node);
673d62cc 757 hlist_add_head(&obj->node, &freelist);
3ac7fe5a
TG
758 break;
759 }
760 }
aef9cb05 761 raw_spin_unlock_irqrestore(&db->lock, flags);
673d62cc
VN
762
763 /* Now free them */
b67bfe0d 764 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
673d62cc
VN
765 hlist_del(&obj->node);
766 free_object(obj);
767 }
768
3ac7fe5a
TG
769 if (cnt > debug_objects_maxchain)
770 debug_objects_maxchain = cnt;
771 }
772}
773
774void debug_check_no_obj_freed(const void *address, unsigned long size)
775{
776 if (debug_objects_enabled)
777 __debug_check_no_obj_freed(address, size);
778}
779#endif
780
781#ifdef CONFIG_DEBUG_FS
782
783static int debug_stats_show(struct seq_file *m, void *v)
784{
785 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
786 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
787 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
788 seq_printf(m, "pool_free :%d\n", obj_pool_free);
789 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
790 seq_printf(m, "pool_used :%d\n", obj_pool_used);
791 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
0cad93c3
WL
792 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
793 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
3ac7fe5a
TG
794 return 0;
795}
796
797static int debug_stats_open(struct inode *inode, struct file *filp)
798{
799 return single_open(filp, debug_stats_show, NULL);
800}
801
802static const struct file_operations debug_stats_fops = {
803 .open = debug_stats_open,
804 .read = seq_read,
805 .llseek = seq_lseek,
806 .release = single_release,
807};
808
809static int __init debug_objects_init_debugfs(void)
810{
811 struct dentry *dbgdir, *dbgstats;
812
813 if (!debug_objects_enabled)
814 return 0;
815
816 dbgdir = debugfs_create_dir("debug_objects", NULL);
817 if (!dbgdir)
818 return -ENOMEM;
819
820 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
821 &debug_stats_fops);
822 if (!dbgstats)
823 goto err;
824
825 return 0;
826
827err:
828 debugfs_remove(dbgdir);
829
830 return -ENOMEM;
831}
832__initcall(debug_objects_init_debugfs);
833
834#else
835static inline void debug_objects_init_debugfs(void) { }
836#endif
837
838#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
839
840/* Random data structure for the self test */
841struct self_test {
842 unsigned long dummy1[6];
843 int static_init;
844 unsigned long dummy2[3];
845};
846
847static __initdata struct debug_obj_descr descr_type_test;
848
b9fdac7f
DC
849static bool __init is_static_object(void *addr)
850{
851 struct self_test *obj = addr;
852
853 return obj->static_init;
854}
855
3ac7fe5a
TG
856/*
857 * fixup_init is called when:
858 * - an active object is initialized
859 */
b1e4d9d8 860static bool __init fixup_init(void *addr, enum debug_obj_state state)
3ac7fe5a
TG
861{
862 struct self_test *obj = addr;
863
864 switch (state) {
865 case ODEBUG_STATE_ACTIVE:
866 debug_object_deactivate(obj, &descr_type_test);
867 debug_object_init(obj, &descr_type_test);
b1e4d9d8 868 return true;
3ac7fe5a 869 default:
b1e4d9d8 870 return false;
3ac7fe5a
TG
871 }
872}
873
874/*
875 * fixup_activate is called when:
876 * - an active object is activated
b9fdac7f 877 * - an unknown non-static object is activated
3ac7fe5a 878 */
b1e4d9d8 879static bool __init fixup_activate(void *addr, enum debug_obj_state state)
3ac7fe5a
TG
880{
881 struct self_test *obj = addr;
882
883 switch (state) {
884 case ODEBUG_STATE_NOTAVAILABLE:
b1e4d9d8 885 return true;
3ac7fe5a
TG
886 case ODEBUG_STATE_ACTIVE:
887 debug_object_deactivate(obj, &descr_type_test);
888 debug_object_activate(obj, &descr_type_test);
b1e4d9d8 889 return true;
3ac7fe5a
TG
890
891 default:
b1e4d9d8 892 return false;
3ac7fe5a
TG
893 }
894}
895
896/*
897 * fixup_destroy is called when:
898 * - an active object is destroyed
899 */
b1e4d9d8 900static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
3ac7fe5a
TG
901{
902 struct self_test *obj = addr;
903
904 switch (state) {
905 case ODEBUG_STATE_ACTIVE:
906 debug_object_deactivate(obj, &descr_type_test);
907 debug_object_destroy(obj, &descr_type_test);
b1e4d9d8 908 return true;
3ac7fe5a 909 default:
b1e4d9d8 910 return false;
3ac7fe5a
TG
911 }
912}
913
914/*
915 * fixup_free is called when:
916 * - an active object is freed
917 */
b1e4d9d8 918static bool __init fixup_free(void *addr, enum debug_obj_state state)
3ac7fe5a
TG
919{
920 struct self_test *obj = addr;
921
922 switch (state) {
923 case ODEBUG_STATE_ACTIVE:
924 debug_object_deactivate(obj, &descr_type_test);
925 debug_object_free(obj, &descr_type_test);
b1e4d9d8 926 return true;
3ac7fe5a 927 default:
b1e4d9d8 928 return false;
3ac7fe5a
TG
929 }
930}
931
1fb2f77c 932static int __init
3ac7fe5a
TG
933check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
934{
935 struct debug_bucket *db;
936 struct debug_obj *obj;
937 unsigned long flags;
938 int res = -EINVAL;
939
940 db = get_bucket((unsigned long) addr);
941
aef9cb05 942 raw_spin_lock_irqsave(&db->lock, flags);
3ac7fe5a
TG
943
944 obj = lookup_object(addr, db);
945 if (!obj && state != ODEBUG_STATE_NONE) {
5cd2b459 946 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
3ac7fe5a
TG
947 goto out;
948 }
949 if (obj && obj->state != state) {
5cd2b459 950 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
3ac7fe5a 951 obj->state, state);
3ac7fe5a
TG
952 goto out;
953 }
954 if (fixups != debug_objects_fixups) {
5cd2b459 955 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
3ac7fe5a 956 fixups, debug_objects_fixups);
3ac7fe5a
TG
957 goto out;
958 }
959 if (warnings != debug_objects_warnings) {
5cd2b459 960 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
3ac7fe5a 961 warnings, debug_objects_warnings);
3ac7fe5a
TG
962 goto out;
963 }
964 res = 0;
965out:
aef9cb05 966 raw_spin_unlock_irqrestore(&db->lock, flags);
3ac7fe5a
TG
967 if (res)
968 debug_objects_enabled = 0;
969 return res;
970}
971
972static __initdata struct debug_obj_descr descr_type_test = {
973 .name = "selftest",
b9fdac7f 974 .is_static_object = is_static_object,
3ac7fe5a
TG
975 .fixup_init = fixup_init,
976 .fixup_activate = fixup_activate,
977 .fixup_destroy = fixup_destroy,
978 .fixup_free = fixup_free,
979};
980
981static __initdata struct self_test obj = { .static_init = 0 };
982
983static void __init debug_objects_selftest(void)
984{
985 int fixups, oldfixups, warnings, oldwarnings;
986 unsigned long flags;
987
988 local_irq_save(flags);
989
990 fixups = oldfixups = debug_objects_fixups;
991 warnings = oldwarnings = debug_objects_warnings;
992 descr_test = &descr_type_test;
993
994 debug_object_init(&obj, &descr_type_test);
995 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
996 goto out;
997 debug_object_activate(&obj, &descr_type_test);
998 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
999 goto out;
1000 debug_object_activate(&obj, &descr_type_test);
1001 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1002 goto out;
1003 debug_object_deactivate(&obj, &descr_type_test);
1004 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1005 goto out;
1006 debug_object_destroy(&obj, &descr_type_test);
1007 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1008 goto out;
1009 debug_object_init(&obj, &descr_type_test);
1010 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1011 goto out;
1012 debug_object_activate(&obj, &descr_type_test);
1013 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1014 goto out;
1015 debug_object_deactivate(&obj, &descr_type_test);
1016 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1017 goto out;
1018 debug_object_free(&obj, &descr_type_test);
1019 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1020 goto out;
1021
1022 obj.static_init = 1;
1023 debug_object_activate(&obj, &descr_type_test);
9f78ff00 1024 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
3ac7fe5a
TG
1025 goto out;
1026 debug_object_init(&obj, &descr_type_test);
1027 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1028 goto out;
1029 debug_object_free(&obj, &descr_type_test);
1030 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1031 goto out;
1032
1033#ifdef CONFIG_DEBUG_OBJECTS_FREE
1034 debug_object_init(&obj, &descr_type_test);
1035 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1036 goto out;
1037 debug_object_activate(&obj, &descr_type_test);
1038 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1039 goto out;
1040 __debug_check_no_obj_freed(&obj, sizeof(obj));
1041 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1042 goto out;
1043#endif
719e4843 1044 pr_info("selftest passed\n");
3ac7fe5a
TG
1045
1046out:
1047 debug_objects_fixups = oldfixups;
1048 debug_objects_warnings = oldwarnings;
1049 descr_test = NULL;
1050
1051 local_irq_restore(flags);
1052}
1053#else
1054static inline void debug_objects_selftest(void) { }
1055#endif
1056
1057/*
1058 * Called during early boot to initialize the hash buckets and link
1059 * the static object pool objects into the poll list. After this call
1060 * the object tracker is fully operational.
1061 */
1062void __init debug_objects_early_init(void)
1063{
1064 int i;
1065
1066 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
aef9cb05 1067 raw_spin_lock_init(&obj_hash[i].lock);
3ac7fe5a
TG
1068
1069 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1070 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1071}
1072
1be1cb7b
TG
1073/*
1074 * Convert the statically allocated objects to dynamic ones:
1075 */
1fb2f77c 1076static int __init debug_objects_replace_static_objects(void)
1be1cb7b
TG
1077{
1078 struct debug_bucket *db = obj_hash;
b67bfe0d 1079 struct hlist_node *tmp;
1be1cb7b
TG
1080 struct debug_obj *obj, *new;
1081 HLIST_HEAD(objects);
1082 int i, cnt = 0;
1083
1084 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1085 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1086 if (!obj)
1087 goto free;
caba4cbb 1088 kmemleak_ignore(obj);
1be1cb7b
TG
1089 hlist_add_head(&obj->node, &objects);
1090 }
1091
1092 /*
1093 * When debug_objects_mem_init() is called we know that only
1094 * one CPU is up, so disabling interrupts is enough
1095 * protection. This avoids the lockdep hell of lock ordering.
1096 */
1097 local_irq_disable();
1098
1099 /* Remove the statically allocated objects from the pool */
b67bfe0d 1100 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
1be1cb7b
TG
1101 hlist_del(&obj->node);
1102 /* Move the allocated objects to the pool */
1103 hlist_move_list(&objects, &obj_pool);
1104
1105 /* Replace the active object references */
1106 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1107 hlist_move_list(&db->list, &objects);
1108
b67bfe0d 1109 hlist_for_each_entry(obj, &objects, node) {
1be1cb7b
TG
1110 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1111 hlist_del(&new->node);
1112 /* copy object data */
1113 *new = *obj;
1114 hlist_add_head(&new->node, &db->list);
1115 cnt++;
1116 }
1117 }
765a5e0c 1118 local_irq_enable();
1be1cb7b 1119
c0f35cc0
FF
1120 pr_debug("%d of %d active objects replaced\n",
1121 cnt, obj_pool_used);
1be1cb7b
TG
1122 return 0;
1123free:
b67bfe0d 1124 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
1be1cb7b
TG
1125 hlist_del(&obj->node);
1126 kmem_cache_free(obj_cache, obj);
1127 }
1128 return -ENOMEM;
1129}
1130
3ac7fe5a
TG
1131/*
1132 * Called after the kmem_caches are functional to setup a dedicated
1133 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1134 * prevents that the debug code is called on kmem_cache_free() for the
1135 * debug tracker objects to avoid recursive calls.
1136 */
1137void __init debug_objects_mem_init(void)
1138{
1139 if (!debug_objects_enabled)
1140 return;
1141
1142 obj_cache = kmem_cache_create("debug_objects_cache",
1143 sizeof (struct debug_obj), 0,
1144 SLAB_DEBUG_OBJECTS, NULL);
1145
1be1cb7b 1146 if (!obj_cache || debug_objects_replace_static_objects()) {
3ac7fe5a 1147 debug_objects_enabled = 0;
1be1cb7b
TG
1148 if (obj_cache)
1149 kmem_cache_destroy(obj_cache);
719e4843 1150 pr_warn("out of memory.\n");
1be1cb7b 1151 } else
3ac7fe5a 1152 debug_objects_selftest();
97dd552e
WL
1153
1154 /*
1155 * Increase the thresholds for allocating and freeing objects
1156 * according to the number of possible CPUs available in the system.
1157 */
1158 debug_objects_pool_size += num_possible_cpus() * 32;
1159 debug_objects_pool_min_level += num_possible_cpus() * 4;
3ac7fe5a 1160}