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