blob: 99308479b1c80276ee814217fa4f7136b1b86906 [file] [log] [blame]
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001/*
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 */
Fabian Frederick719e4842014-06-04 16:06:04 -070010
11#define pr_fmt(fmt) "ODEBUG: " fmt
12
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070013#include <linux/debugobjects.h>
14#include <linux/interrupt.h>
Alexey Dobriyand43c36d2009-10-07 17:09:06 +040015#include <linux/sched.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010016#include <linux/sched/task_stack.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070017#include <linux/seq_file.h>
18#include <linux/debugfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070020#include <linux/hash.h>
Waiman Longcaba4cb2017-08-14 09:52:13 -040021#include <linux/kmemleak.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070022
23#define ODEBUG_HASH_BITS 14
24#define ODEBUG_HASH_SIZE (1 << ODEBUG_HASH_BITS)
25
Christian Borntraeger0b6ec8c2016-01-27 15:37:58 +010026#define ODEBUG_POOL_SIZE 1024
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070027#define ODEBUG_POOL_MIN_LEVEL 256
28
29#define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
30#define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
31#define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
32
33struct debug_bucket {
34 struct hlist_head list;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010035 raw_spinlock_t lock;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070036};
37
38static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
39
Thomas Gleixner1be1cb72009-03-16 18:53:18 +010040static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070041
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010042static DEFINE_RAW_SPINLOCK(pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070043
44static HLIST_HEAD(obj_pool);
45
46static int obj_pool_min_free = ODEBUG_POOL_SIZE;
47static int obj_pool_free = ODEBUG_POOL_SIZE;
48static int obj_pool_used;
49static int obj_pool_max_used;
50static struct kmem_cache *obj_cache;
51
52static int debug_objects_maxchain __read_mostly;
53static int debug_objects_fixups __read_mostly;
54static int debug_objects_warnings __read_mostly;
Ingo Molnar3ae70202008-11-26 10:02:00 +010055static int debug_objects_enabled __read_mostly
56 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
Waiman Long97dd5522017-01-05 15:17:04 -050057static int debug_objects_pool_size __read_mostly
58 = ODEBUG_POOL_SIZE;
59static int debug_objects_pool_min_level __read_mostly
60 = ODEBUG_POOL_MIN_LEVEL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070061static struct debug_obj_descr *descr_test __read_mostly;
62
Waiman Longc4b73aa2017-01-05 15:17:03 -050063/*
Waiman Long0cad93c2017-02-07 16:40:30 -050064 * Track numbers of kmem_cache_alloc()/free() calls done.
Waiman Longc4b73aa2017-01-05 15:17:03 -050065 */
Waiman Long0cad93c2017-02-07 16:40:30 -050066static int debug_objects_allocated;
Waiman Longc4b73aa2017-01-05 15:17:03 -050067static int debug_objects_freed;
68
Thomas Gleixner337fff82009-03-16 10:04:53 +010069static void free_obj_work(struct work_struct *work);
70static DECLARE_WORK(debug_obj_work, free_obj_work);
71
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070072static int __init enable_object_debug(char *str)
73{
74 debug_objects_enabled = 1;
75 return 0;
76}
Kyle McMartin3e8ebb52009-03-01 20:41:41 -050077
78static int __init disable_object_debug(char *str)
79{
80 debug_objects_enabled = 0;
81 return 0;
82}
83
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070084early_param("debug_objects", enable_object_debug);
Kyle McMartin3e8ebb52009-03-01 20:41:41 -050085early_param("no_debug_objects", disable_object_debug);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070086
87static const char *obj_states[ODEBUG_STATE_MAX] = {
88 [ODEBUG_STATE_NONE] = "none",
89 [ODEBUG_STATE_INIT] = "initialized",
90 [ODEBUG_STATE_INACTIVE] = "inactive",
91 [ODEBUG_STATE_ACTIVE] = "active",
92 [ODEBUG_STATE_DESTROYED] = "destroyed",
93 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
94};
95
Thomas Gleixner1fda1072012-04-11 11:52:18 +020096static void fill_pool(void)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070097{
98 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
99 struct debug_obj *new;
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200100 unsigned long flags;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700101
Waiman Long97dd5522017-01-05 15:17:04 -0500102 if (likely(obj_pool_free >= debug_objects_pool_min_level))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200103 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700104
105 if (unlikely(!obj_cache))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200106 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700107
Waiman Long97dd5522017-01-05 15:17:04 -0500108 while (obj_pool_free < debug_objects_pool_min_level) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700109
110 new = kmem_cache_zalloc(obj_cache, gfp);
111 if (!new)
Dan Carpenter33408082012-04-18 14:28:10 +0300112 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700113
Waiman Longcaba4cb2017-08-14 09:52:13 -0400114 kmemleak_ignore(new);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100115 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700116 hlist_add_head(&new->node, &obj_pool);
Waiman Long0cad93c2017-02-07 16:40:30 -0500117 debug_objects_allocated++;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700118 obj_pool_free++;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100119 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700120 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700121}
122
123/*
124 * Lookup an object in the hash bucket.
125 */
126static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
127{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700128 struct debug_obj *obj;
129 int cnt = 0;
130
Sasha Levinb67bfe02013-02-27 17:06:00 -0800131 hlist_for_each_entry(obj, &b->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700132 cnt++;
133 if (obj->object == addr)
134 return obj;
135 }
136 if (cnt > debug_objects_maxchain)
137 debug_objects_maxchain = cnt;
138
139 return NULL;
140}
141
142/*
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200143 * Allocate a new object. If the pool is empty, switch off the debugger.
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200144 * Must be called with interrupts disabled.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700145 */
146static struct debug_obj *
147alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
148{
149 struct debug_obj *obj = NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700150
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100151 raw_spin_lock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700152 if (obj_pool.first) {
153 obj = hlist_entry(obj_pool.first, typeof(*obj), node);
154
155 obj->object = addr;
156 obj->descr = descr;
157 obj->state = ODEBUG_STATE_NONE;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400158 obj->astate = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700159 hlist_del(&obj->node);
160
161 hlist_add_head(&obj->node, &b->list);
162
163 obj_pool_used++;
164 if (obj_pool_used > obj_pool_max_used)
165 obj_pool_max_used = obj_pool_used;
166
167 obj_pool_free--;
168 if (obj_pool_free < obj_pool_min_free)
169 obj_pool_min_free = obj_pool_free;
170 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100171 raw_spin_unlock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700172
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700173 return obj;
174}
175
176/*
Thomas Gleixner337fff82009-03-16 10:04:53 +0100177 * workqueue function to free objects.
Waiman Long858274b2017-01-05 15:17:05 -0500178 *
179 * To reduce contention on the global pool_lock, the actual freeing of
180 * debug objects will be delayed if the pool_lock is busy. We also free
181 * the objects in a batch of 4 for each lock/unlock cycle.
Thomas Gleixner337fff82009-03-16 10:04:53 +0100182 */
Waiman Long858274b2017-01-05 15:17:05 -0500183#define ODEBUG_FREE_BATCH 4
184
Thomas Gleixner337fff82009-03-16 10:04:53 +0100185static void free_obj_work(struct work_struct *work)
186{
Waiman Long858274b2017-01-05 15:17:05 -0500187 struct debug_obj *objs[ODEBUG_FREE_BATCH];
Thomas Gleixner337fff82009-03-16 10:04:53 +0100188 unsigned long flags;
Waiman Long858274b2017-01-05 15:17:05 -0500189 int i;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100190
Waiman Long858274b2017-01-05 15:17:05 -0500191 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
192 return;
193 while (obj_pool_free >= debug_objects_pool_size + ODEBUG_FREE_BATCH) {
194 for (i = 0; i < ODEBUG_FREE_BATCH; i++) {
195 objs[i] = hlist_entry(obj_pool.first,
196 typeof(*objs[0]), node);
197 hlist_del(&objs[i]->node);
198 }
199
200 obj_pool_free -= ODEBUG_FREE_BATCH;
201 debug_objects_freed += ODEBUG_FREE_BATCH;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100202 /*
203 * We release pool_lock across kmem_cache_free() to
204 * avoid contention on pool_lock.
205 */
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100206 raw_spin_unlock_irqrestore(&pool_lock, flags);
Waiman Long858274b2017-01-05 15:17:05 -0500207 for (i = 0; i < ODEBUG_FREE_BATCH; i++)
208 kmem_cache_free(obj_cache, objs[i]);
209 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
210 return;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100211 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100212 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100213}
214
215/*
216 * Put the object back into the pool and schedule work to free objects
217 * if necessary.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700218 */
219static void free_object(struct debug_obj *obj)
220{
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200221 unsigned long flags;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100222 int sched = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700223
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100224 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100225 /*
226 * schedule work when the pool is filled and the cache is
227 * initialized:
228 */
Waiman Long97dd5522017-01-05 15:17:04 -0500229 if (obj_pool_free > debug_objects_pool_size && obj_cache)
Tejun Heo7092dff2016-09-16 15:49:34 -0400230 sched = 1;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100231 hlist_add_head(&obj->node, &obj_pool);
232 obj_pool_free++;
233 obj_pool_used--;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100234 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100235 if (sched)
236 schedule_work(&debug_obj_work);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700237}
238
239/*
240 * We run out of memory. That means we probably have tons of objects
241 * allocated.
242 */
243static void debug_objects_oom(void)
244{
245 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800246 struct hlist_node *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200247 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700248 struct debug_obj *obj;
249 unsigned long flags;
250 int i;
251
Fabian Frederick719e4842014-06-04 16:06:04 -0700252 pr_warn("Out of memory. ODEBUG disabled\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700253
254 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100255 raw_spin_lock_irqsave(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200256 hlist_move_list(&db->list, &freelist);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100257 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200258
259 /* Now free them */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800260 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700261 hlist_del(&obj->node);
262 free_object(obj);
263 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700264 }
265}
266
267/*
268 * We use the pfn of the address for the hash. That way we can check
269 * for freed objects simply by checking the affected bucket.
270 */
271static struct debug_bucket *get_bucket(unsigned long addr)
272{
273 unsigned long hash;
274
275 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
276 return &obj_hash[hash];
277}
278
279static void debug_print_object(struct debug_obj *obj, char *msg)
280{
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100281 struct debug_obj_descr *descr = obj->descr;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700282 static int limit;
283
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100284 if (limit < 5 && descr != descr_test) {
285 void *hint = descr->debug_hint ?
286 descr->debug_hint(obj->object) : NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700287 limit++;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400288 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100289 "object type: %s hint: %pS\n",
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400290 msg, obj_states[obj->state], obj->astate,
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100291 descr->name, hint);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700292 }
293 debug_objects_warnings++;
294}
295
296/*
297 * Try to repair the damage, so we have a better chance to get useful
298 * debug output.
299 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700300static bool
301debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700302 void * addr, enum debug_obj_state state)
303{
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700304 if (fixup && fixup(addr, state)) {
305 debug_objects_fixups++;
306 return true;
307 }
308 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700309}
310
311static void debug_object_is_on_stack(void *addr, int onstack)
312{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700313 int is_on_stack;
314 static int limit;
315
316 if (limit > 4)
317 return;
318
FUJITA Tomonori8b05c7e2008-07-23 21:26:53 -0700319 is_on_stack = object_is_on_stack(addr);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700320 if (is_on_stack == onstack)
321 return;
322
323 limit++;
324 if (is_on_stack)
Joel Fernandes (Google)8d015a32018-07-23 14:25:31 -0700325 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
326 task_stack_page(current));
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700327 else
Joel Fernandes (Google)8d015a32018-07-23 14:25:31 -0700328 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
329 task_stack_page(current));
330
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700331 WARN_ON(1);
332}
333
334static void
335__debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
336{
337 enum debug_obj_state state;
338 struct debug_bucket *db;
339 struct debug_obj *obj;
340 unsigned long flags;
341
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200342 fill_pool();
343
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700344 db = get_bucket((unsigned long) addr);
345
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100346 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700347
348 obj = lookup_object(addr, db);
349 if (!obj) {
350 obj = alloc_object(addr, db, descr);
351 if (!obj) {
352 debug_objects_enabled = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100353 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700354 debug_objects_oom();
355 return;
356 }
357 debug_object_is_on_stack(addr, onstack);
358 }
359
360 switch (obj->state) {
361 case ODEBUG_STATE_NONE:
362 case ODEBUG_STATE_INIT:
363 case ODEBUG_STATE_INACTIVE:
364 obj->state = ODEBUG_STATE_INIT;
365 break;
366
367 case ODEBUG_STATE_ACTIVE:
368 debug_print_object(obj, "init");
369 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100370 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700371 debug_object_fixup(descr->fixup_init, addr, state);
372 return;
373
374 case ODEBUG_STATE_DESTROYED:
375 debug_print_object(obj, "init");
376 break;
377 default:
378 break;
379 }
380
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100381 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700382}
383
384/**
385 * debug_object_init - debug checks when an object is initialized
386 * @addr: address of the object
387 * @descr: pointer to an object specific debug description structure
388 */
389void debug_object_init(void *addr, struct debug_obj_descr *descr)
390{
391 if (!debug_objects_enabled)
392 return;
393
394 __debug_object_init(addr, descr, 0);
395}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800396EXPORT_SYMBOL_GPL(debug_object_init);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700397
398/**
399 * debug_object_init_on_stack - debug checks when an object on stack is
400 * initialized
401 * @addr: address of the object
402 * @descr: pointer to an object specific debug description structure
403 */
404void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
405{
406 if (!debug_objects_enabled)
407 return;
408
409 __debug_object_init(addr, descr, 1);
410}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800411EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700412
413/**
414 * debug_object_activate - debug checks when an object is activated
415 * @addr: address of the object
416 * @descr: pointer to an object specific debug description structure
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700417 * Returns 0 for success, -EINVAL for check failed.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700418 */
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700419int debug_object_activate(void *addr, struct debug_obj_descr *descr)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700420{
421 enum debug_obj_state state;
422 struct debug_bucket *db;
423 struct debug_obj *obj;
424 unsigned long flags;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700425 int ret;
Stephen Boydfeac18d2011-11-07 19:48:26 -0800426 struct debug_obj o = { .object = addr,
427 .state = ODEBUG_STATE_NOTAVAILABLE,
428 .descr = descr };
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700429
430 if (!debug_objects_enabled)
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700431 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700432
433 db = get_bucket((unsigned long) addr);
434
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100435 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700436
437 obj = lookup_object(addr, db);
438 if (obj) {
439 switch (obj->state) {
440 case ODEBUG_STATE_INIT:
441 case ODEBUG_STATE_INACTIVE:
442 obj->state = ODEBUG_STATE_ACTIVE;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700443 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700444 break;
445
446 case ODEBUG_STATE_ACTIVE:
447 debug_print_object(obj, "activate");
448 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100449 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700450 ret = debug_object_fixup(descr->fixup_activate, addr, state);
Du, Changbine7a8e782016-05-19 17:09:23 -0700451 return ret ? 0 : -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700452
453 case ODEBUG_STATE_DESTROYED:
454 debug_print_object(obj, "activate");
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700455 ret = -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700456 break;
457 default:
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700458 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700459 break;
460 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100461 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700462 return ret;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700463 }
464
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100465 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700466 /*
Du, Changbinb9fdac72016-05-19 17:09:41 -0700467 * We are here when a static object is activated. We
468 * let the type specific code confirm whether this is
469 * true or not. if true, we just make sure that the
470 * static object is tracked in the object tracker. If
471 * not, this must be a bug, so we try to fix it up.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700472 */
Du, Changbinb9fdac72016-05-19 17:09:41 -0700473 if (descr->is_static_object && descr->is_static_object(addr)) {
474 /* track this static object */
475 debug_object_init(addr, descr);
476 debug_object_activate(addr, descr);
477 } else {
Stephen Boydfeac18d2011-11-07 19:48:26 -0800478 debug_print_object(&o, "activate");
Du, Changbinb9fdac72016-05-19 17:09:41 -0700479 ret = debug_object_fixup(descr->fixup_activate, addr,
480 ODEBUG_STATE_NOTAVAILABLE);
481 return ret ? 0 : -EINVAL;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700482 }
483 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700484}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800485EXPORT_SYMBOL_GPL(debug_object_activate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700486
487/**
488 * debug_object_deactivate - debug checks when an object is deactivated
489 * @addr: address of the object
490 * @descr: pointer to an object specific debug description structure
491 */
492void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
493{
494 struct debug_bucket *db;
495 struct debug_obj *obj;
496 unsigned long flags;
497
498 if (!debug_objects_enabled)
499 return;
500
501 db = get_bucket((unsigned long) addr);
502
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100503 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700504
505 obj = lookup_object(addr, db);
506 if (obj) {
507 switch (obj->state) {
508 case ODEBUG_STATE_INIT:
509 case ODEBUG_STATE_INACTIVE:
510 case ODEBUG_STATE_ACTIVE:
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400511 if (!obj->astate)
512 obj->state = ODEBUG_STATE_INACTIVE;
513 else
514 debug_print_object(obj, "deactivate");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700515 break;
516
517 case ODEBUG_STATE_DESTROYED:
518 debug_print_object(obj, "deactivate");
519 break;
520 default:
521 break;
522 }
523 } else {
524 struct debug_obj o = { .object = addr,
525 .state = ODEBUG_STATE_NOTAVAILABLE,
526 .descr = descr };
527
528 debug_print_object(&o, "deactivate");
529 }
530
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100531 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700532}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800533EXPORT_SYMBOL_GPL(debug_object_deactivate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700534
535/**
536 * debug_object_destroy - debug checks when an object is destroyed
537 * @addr: address of the object
538 * @descr: pointer to an object specific debug description structure
539 */
540void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
541{
542 enum debug_obj_state state;
543 struct debug_bucket *db;
544 struct debug_obj *obj;
545 unsigned long flags;
546
547 if (!debug_objects_enabled)
548 return;
549
550 db = get_bucket((unsigned long) addr);
551
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100552 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700553
554 obj = lookup_object(addr, db);
555 if (!obj)
556 goto out_unlock;
557
558 switch (obj->state) {
559 case ODEBUG_STATE_NONE:
560 case ODEBUG_STATE_INIT:
561 case ODEBUG_STATE_INACTIVE:
562 obj->state = ODEBUG_STATE_DESTROYED;
563 break;
564 case ODEBUG_STATE_ACTIVE:
565 debug_print_object(obj, "destroy");
566 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100567 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700568 debug_object_fixup(descr->fixup_destroy, addr, state);
569 return;
570
571 case ODEBUG_STATE_DESTROYED:
572 debug_print_object(obj, "destroy");
573 break;
574 default:
575 break;
576 }
577out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100578 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700579}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800580EXPORT_SYMBOL_GPL(debug_object_destroy);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700581
582/**
583 * debug_object_free - debug checks when an object is freed
584 * @addr: address of the object
585 * @descr: pointer to an object specific debug description structure
586 */
587void debug_object_free(void *addr, struct debug_obj_descr *descr)
588{
589 enum debug_obj_state state;
590 struct debug_bucket *db;
591 struct debug_obj *obj;
592 unsigned long flags;
593
594 if (!debug_objects_enabled)
595 return;
596
597 db = get_bucket((unsigned long) addr);
598
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100599 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700600
601 obj = lookup_object(addr, db);
602 if (!obj)
603 goto out_unlock;
604
605 switch (obj->state) {
606 case ODEBUG_STATE_ACTIVE:
607 debug_print_object(obj, "free");
608 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100609 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700610 debug_object_fixup(descr->fixup_free, addr, state);
611 return;
612 default:
613 hlist_del(&obj->node);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100614 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700615 free_object(obj);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200616 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700617 }
618out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100619 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700620}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800621EXPORT_SYMBOL_GPL(debug_object_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700622
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400623/**
Christine Chanb84d4352011-11-07 19:48:27 -0800624 * debug_object_assert_init - debug checks when object should be init-ed
625 * @addr: address of the object
626 * @descr: pointer to an object specific debug description structure
627 */
628void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
629{
630 struct debug_bucket *db;
631 struct debug_obj *obj;
632 unsigned long flags;
633
634 if (!debug_objects_enabled)
635 return;
636
637 db = get_bucket((unsigned long) addr);
638
639 raw_spin_lock_irqsave(&db->lock, flags);
640
641 obj = lookup_object(addr, db);
642 if (!obj) {
643 struct debug_obj o = { .object = addr,
644 .state = ODEBUG_STATE_NOTAVAILABLE,
645 .descr = descr };
646
647 raw_spin_unlock_irqrestore(&db->lock, flags);
648 /*
Du, Changbinb9fdac72016-05-19 17:09:41 -0700649 * Maybe the object is static, and we let the type specific
650 * code confirm. Track this static object if true, else invoke
651 * fixup.
Christine Chanb84d4352011-11-07 19:48:27 -0800652 */
Du, Changbinb9fdac72016-05-19 17:09:41 -0700653 if (descr->is_static_object && descr->is_static_object(addr)) {
654 /* Track this static object */
655 debug_object_init(addr, descr);
656 } else {
Christine Chanb84d4352011-11-07 19:48:27 -0800657 debug_print_object(&o, "assert_init");
Du, Changbinb9fdac72016-05-19 17:09:41 -0700658 debug_object_fixup(descr->fixup_assert_init, addr,
659 ODEBUG_STATE_NOTAVAILABLE);
660 }
Christine Chanb84d4352011-11-07 19:48:27 -0800661 return;
662 }
663
664 raw_spin_unlock_irqrestore(&db->lock, flags);
665}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800666EXPORT_SYMBOL_GPL(debug_object_assert_init);
Christine Chanb84d4352011-11-07 19:48:27 -0800667
668/**
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400669 * debug_object_active_state - debug checks object usage state machine
670 * @addr: address of the object
671 * @descr: pointer to an object specific debug description structure
672 * @expect: expected state
673 * @next: state to move to if expected state is found
674 */
675void
676debug_object_active_state(void *addr, struct debug_obj_descr *descr,
677 unsigned int expect, unsigned int next)
678{
679 struct debug_bucket *db;
680 struct debug_obj *obj;
681 unsigned long flags;
682
683 if (!debug_objects_enabled)
684 return;
685
686 db = get_bucket((unsigned long) addr);
687
688 raw_spin_lock_irqsave(&db->lock, flags);
689
690 obj = lookup_object(addr, db);
691 if (obj) {
692 switch (obj->state) {
693 case ODEBUG_STATE_ACTIVE:
694 if (obj->astate == expect)
695 obj->astate = next;
696 else
697 debug_print_object(obj, "active_state");
698 break;
699
700 default:
701 debug_print_object(obj, "active_state");
702 break;
703 }
704 } else {
705 struct debug_obj o = { .object = addr,
706 .state = ODEBUG_STATE_NOTAVAILABLE,
707 .descr = descr };
708
709 debug_print_object(&o, "active_state");
710 }
711
712 raw_spin_unlock_irqrestore(&db->lock, flags);
713}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800714EXPORT_SYMBOL_GPL(debug_object_active_state);
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400715
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700716#ifdef CONFIG_DEBUG_OBJECTS_FREE
717static void __debug_check_no_obj_freed(const void *address, unsigned long size)
718{
719 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800720 struct hlist_node *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200721 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700722 struct debug_obj_descr *descr;
723 enum debug_obj_state state;
724 struct debug_bucket *db;
725 struct debug_obj *obj;
726 int cnt;
727
728 saddr = (unsigned long) address;
729 eaddr = saddr + size;
730 paddr = saddr & ODEBUG_CHUNK_MASK;
731 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
732 chunks >>= ODEBUG_CHUNK_SHIFT;
733
734 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
735 db = get_bucket(paddr);
736
737repeat:
738 cnt = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100739 raw_spin_lock_irqsave(&db->lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800740 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700741 cnt++;
742 oaddr = (unsigned long) obj->object;
743 if (oaddr < saddr || oaddr >= eaddr)
744 continue;
745
746 switch (obj->state) {
747 case ODEBUG_STATE_ACTIVE:
748 debug_print_object(obj, "free");
749 descr = obj->descr;
750 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100751 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700752 debug_object_fixup(descr->fixup_free,
753 (void *) oaddr, state);
754 goto repeat;
755 default:
756 hlist_del(&obj->node);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200757 hlist_add_head(&obj->node, &freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700758 break;
759 }
760 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100761 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200762
763 /* Now free them */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800764 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200765 hlist_del(&obj->node);
766 free_object(obj);
767 }
768
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700769 if (cnt > debug_objects_maxchain)
770 debug_objects_maxchain = cnt;
771 }
772}
773
774void debug_check_no_obj_freed(const void *address, unsigned long size)
775{
776 if (debug_objects_enabled)
777 __debug_check_no_obj_freed(address, size);
778}
779#endif
780
781#ifdef CONFIG_DEBUG_FS
782
783static int debug_stats_show(struct seq_file *m, void *v)
784{
785 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
786 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
787 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
788 seq_printf(m, "pool_free :%d\n", obj_pool_free);
789 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
790 seq_printf(m, "pool_used :%d\n", obj_pool_used);
791 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
Waiman Long0cad93c2017-02-07 16:40:30 -0500792 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
793 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700794 return 0;
795}
796
797static int debug_stats_open(struct inode *inode, struct file *filp)
798{
799 return single_open(filp, debug_stats_show, NULL);
800}
801
802static const struct file_operations debug_stats_fops = {
803 .open = debug_stats_open,
804 .read = seq_read,
805 .llseek = seq_lseek,
806 .release = single_release,
807};
808
809static int __init debug_objects_init_debugfs(void)
810{
811 struct dentry *dbgdir, *dbgstats;
812
813 if (!debug_objects_enabled)
814 return 0;
815
816 dbgdir = debugfs_create_dir("debug_objects", NULL);
817 if (!dbgdir)
818 return -ENOMEM;
819
820 dbgstats = debugfs_create_file("stats", 0444, dbgdir, NULL,
821 &debug_stats_fops);
822 if (!dbgstats)
823 goto err;
824
825 return 0;
826
827err:
828 debugfs_remove(dbgdir);
829
830 return -ENOMEM;
831}
832__initcall(debug_objects_init_debugfs);
833
834#else
835static inline void debug_objects_init_debugfs(void) { }
836#endif
837
838#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
839
840/* Random data structure for the self test */
841struct self_test {
842 unsigned long dummy1[6];
843 int static_init;
844 unsigned long dummy2[3];
845};
846
847static __initdata struct debug_obj_descr descr_type_test;
848
Du, Changbinb9fdac72016-05-19 17:09:41 -0700849static bool __init is_static_object(void *addr)
850{
851 struct self_test *obj = addr;
852
853 return obj->static_init;
854}
855
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700856/*
857 * fixup_init is called when:
858 * - an active object is initialized
859 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700860static bool __init fixup_init(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700861{
862 struct self_test *obj = addr;
863
864 switch (state) {
865 case ODEBUG_STATE_ACTIVE:
866 debug_object_deactivate(obj, &descr_type_test);
867 debug_object_init(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700868 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700869 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700870 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700871 }
872}
873
874/*
875 * fixup_activate is called when:
876 * - an active object is activated
Du, Changbinb9fdac72016-05-19 17:09:41 -0700877 * - an unknown non-static object is activated
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700878 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700879static bool __init fixup_activate(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700880{
881 struct self_test *obj = addr;
882
883 switch (state) {
884 case ODEBUG_STATE_NOTAVAILABLE:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700885 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700886 case ODEBUG_STATE_ACTIVE:
887 debug_object_deactivate(obj, &descr_type_test);
888 debug_object_activate(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700889 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700890
891 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700892 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700893 }
894}
895
896/*
897 * fixup_destroy is called when:
898 * - an active object is destroyed
899 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700900static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700901{
902 struct self_test *obj = addr;
903
904 switch (state) {
905 case ODEBUG_STATE_ACTIVE:
906 debug_object_deactivate(obj, &descr_type_test);
907 debug_object_destroy(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700908 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700909 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700910 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700911 }
912}
913
914/*
915 * fixup_free is called when:
916 * - an active object is freed
917 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700918static bool __init fixup_free(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700919{
920 struct self_test *obj = addr;
921
922 switch (state) {
923 case ODEBUG_STATE_ACTIVE:
924 debug_object_deactivate(obj, &descr_type_test);
925 debug_object_free(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700926 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700927 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700928 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700929 }
930}
931
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +0100932static int __init
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700933check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
934{
935 struct debug_bucket *db;
936 struct debug_obj *obj;
937 unsigned long flags;
938 int res = -EINVAL;
939
940 db = get_bucket((unsigned long) addr);
941
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100942 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700943
944 obj = lookup_object(addr, db);
945 if (!obj && state != ODEBUG_STATE_NONE) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700946 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700947 goto out;
948 }
949 if (obj && obj->state != state) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700950 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700951 obj->state, state);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700952 goto out;
953 }
954 if (fixups != debug_objects_fixups) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700955 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700956 fixups, debug_objects_fixups);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700957 goto out;
958 }
959 if (warnings != debug_objects_warnings) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -0700960 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700961 warnings, debug_objects_warnings);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700962 goto out;
963 }
964 res = 0;
965out:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100966 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700967 if (res)
968 debug_objects_enabled = 0;
969 return res;
970}
971
972static __initdata struct debug_obj_descr descr_type_test = {
973 .name = "selftest",
Du, Changbinb9fdac72016-05-19 17:09:41 -0700974 .is_static_object = is_static_object,
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700975 .fixup_init = fixup_init,
976 .fixup_activate = fixup_activate,
977 .fixup_destroy = fixup_destroy,
978 .fixup_free = fixup_free,
979};
980
981static __initdata struct self_test obj = { .static_init = 0 };
982
983static void __init debug_objects_selftest(void)
984{
985 int fixups, oldfixups, warnings, oldwarnings;
986 unsigned long flags;
987
988 local_irq_save(flags);
989
990 fixups = oldfixups = debug_objects_fixups;
991 warnings = oldwarnings = debug_objects_warnings;
992 descr_test = &descr_type_test;
993
994 debug_object_init(&obj, &descr_type_test);
995 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
996 goto out;
997 debug_object_activate(&obj, &descr_type_test);
998 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
999 goto out;
1000 debug_object_activate(&obj, &descr_type_test);
1001 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1002 goto out;
1003 debug_object_deactivate(&obj, &descr_type_test);
1004 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1005 goto out;
1006 debug_object_destroy(&obj, &descr_type_test);
1007 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1008 goto out;
1009 debug_object_init(&obj, &descr_type_test);
1010 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1011 goto out;
1012 debug_object_activate(&obj, &descr_type_test);
1013 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1014 goto out;
1015 debug_object_deactivate(&obj, &descr_type_test);
1016 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1017 goto out;
1018 debug_object_free(&obj, &descr_type_test);
1019 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1020 goto out;
1021
1022 obj.static_init = 1;
1023 debug_object_activate(&obj, &descr_type_test);
Stephen Boyd9f78ff02012-03-05 14:59:17 -08001024 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001025 goto out;
1026 debug_object_init(&obj, &descr_type_test);
1027 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1028 goto out;
1029 debug_object_free(&obj, &descr_type_test);
1030 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1031 goto out;
1032
1033#ifdef CONFIG_DEBUG_OBJECTS_FREE
1034 debug_object_init(&obj, &descr_type_test);
1035 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1036 goto out;
1037 debug_object_activate(&obj, &descr_type_test);
1038 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1039 goto out;
1040 __debug_check_no_obj_freed(&obj, sizeof(obj));
1041 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1042 goto out;
1043#endif
Fabian Frederick719e4842014-06-04 16:06:04 -07001044 pr_info("selftest passed\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001045
1046out:
1047 debug_objects_fixups = oldfixups;
1048 debug_objects_warnings = oldwarnings;
1049 descr_test = NULL;
1050
1051 local_irq_restore(flags);
1052}
1053#else
1054static inline void debug_objects_selftest(void) { }
1055#endif
1056
1057/*
1058 * Called during early boot to initialize the hash buckets and link
1059 * the static object pool objects into the poll list. After this call
1060 * the object tracker is fully operational.
1061 */
1062void __init debug_objects_early_init(void)
1063{
1064 int i;
1065
1066 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001067 raw_spin_lock_init(&obj_hash[i].lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001068
1069 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1070 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1071}
1072
1073/*
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001074 * Convert the statically allocated objects to dynamic ones:
1075 */
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001076static int __init debug_objects_replace_static_objects(void)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001077{
1078 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001079 struct hlist_node *tmp;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001080 struct debug_obj *obj, *new;
1081 HLIST_HEAD(objects);
1082 int i, cnt = 0;
1083
1084 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1085 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1086 if (!obj)
1087 goto free;
Waiman Longcaba4cb2017-08-14 09:52:13 -04001088 kmemleak_ignore(obj);
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001089 hlist_add_head(&obj->node, &objects);
1090 }
1091
1092 /*
1093 * When debug_objects_mem_init() is called we know that only
1094 * one CPU is up, so disabling interrupts is enough
1095 * protection. This avoids the lockdep hell of lock ordering.
1096 */
1097 local_irq_disable();
1098
1099 /* Remove the statically allocated objects from the pool */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001100 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001101 hlist_del(&obj->node);
1102 /* Move the allocated objects to the pool */
1103 hlist_move_list(&objects, &obj_pool);
1104
1105 /* Replace the active object references */
1106 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1107 hlist_move_list(&db->list, &objects);
1108
Sasha Levinb67bfe02013-02-27 17:06:00 -08001109 hlist_for_each_entry(obj, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001110 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1111 hlist_del(&new->node);
1112 /* copy object data */
1113 *new = *obj;
1114 hlist_add_head(&new->node, &db->list);
1115 cnt++;
1116 }
1117 }
Thomas Gleixner765a5e02012-04-11 11:54:27 +02001118 local_irq_enable();
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001119
Fabian Frederickc0f35cc2014-06-04 16:06:05 -07001120 pr_debug("%d of %d active objects replaced\n",
1121 cnt, obj_pool_used);
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001122 return 0;
1123free:
Sasha Levinb67bfe02013-02-27 17:06:00 -08001124 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001125 hlist_del(&obj->node);
1126 kmem_cache_free(obj_cache, obj);
1127 }
1128 return -ENOMEM;
1129}
1130
1131/*
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001132 * Called after the kmem_caches are functional to setup a dedicated
1133 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1134 * prevents that the debug code is called on kmem_cache_free() for the
1135 * debug tracker objects to avoid recursive calls.
1136 */
1137void __init debug_objects_mem_init(void)
1138{
1139 if (!debug_objects_enabled)
1140 return;
1141
1142 obj_cache = kmem_cache_create("debug_objects_cache",
1143 sizeof (struct debug_obj), 0,
1144 SLAB_DEBUG_OBJECTS, NULL);
1145
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001146 if (!obj_cache || debug_objects_replace_static_objects()) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001147 debug_objects_enabled = 0;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001148 if (obj_cache)
1149 kmem_cache_destroy(obj_cache);
Fabian Frederick719e4842014-06-04 16:06:04 -07001150 pr_warn("out of memory.\n");
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001151 } else
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001152 debug_objects_selftest();
Waiman Long97dd5522017-01-05 15:17:04 -05001153
1154 /*
1155 * Increase the thresholds for allocating and freeing objects
1156 * according to the number of possible CPUs available in the system.
1157 */
1158 debug_objects_pool_size += num_possible_cpus() * 32;
1159 debug_objects_pool_min_level += num_possible_cpus() * 4;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001160}