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