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