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