blob: 38c23b528f6fb810450dad84158290edb61f5e75 [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
Waiman Longd86998b2019-05-20 10:14:46 -040028#define ODEBUG_POOL_PERCPU_SIZE 64
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070029
30#define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
31#define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
32#define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
33
34struct debug_bucket {
35 struct hlist_head list;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010036 raw_spinlock_t lock;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070037};
38
Waiman Longd86998b2019-05-20 10:14:46 -040039/*
40 * Debug object percpu free list
41 * Access is protected by disabling irq
42 */
43struct debug_percpu_free {
44 struct hlist_head free_objs;
45 int obj_free;
46};
47
48static DEFINE_PER_CPU(struct debug_percpu_free, percpu_obj_pool);
49
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070050static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
51
Thomas Gleixner1be1cb72009-03-16 18:53:18 +010052static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070053
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010054static DEFINE_RAW_SPINLOCK(pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070055
56static HLIST_HEAD(obj_pool);
Yang Shi36c4ead2018-02-06 07:18:26 +080057static HLIST_HEAD(obj_to_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070058
Waiman Longd86998b2019-05-20 10:14:46 -040059/*
60 * Because of the presence of percpu free pools, obj_pool_free will
61 * under-count those in the percpu free pools. Similarly, obj_pool_used
62 * will over-count those in the percpu free pools. Adjustments will be
63 * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
64 * can be off.
65 */
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070066static int obj_pool_min_free = ODEBUG_POOL_SIZE;
67static int obj_pool_free = ODEBUG_POOL_SIZE;
68static int obj_pool_used;
69static int obj_pool_max_used;
Yang Shi36c4ead2018-02-06 07:18:26 +080070/* The number of objs on the global free list */
71static int obj_nr_tofree;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070072
73static int debug_objects_maxchain __read_mostly;
Arnd Bergmann163cf842018-03-13 14:18:46 +010074static int __maybe_unused debug_objects_maxchecked __read_mostly;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070075static int debug_objects_fixups __read_mostly;
76static int debug_objects_warnings __read_mostly;
Ingo Molnar3ae70202008-11-26 10:02:00 +010077static int debug_objects_enabled __read_mostly
78 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
Waiman Long97dd5522017-01-05 15:17:04 -050079static int debug_objects_pool_size __read_mostly
80 = ODEBUG_POOL_SIZE;
81static int debug_objects_pool_min_level __read_mostly
82 = ODEBUG_POOL_MIN_LEVEL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070083static struct debug_obj_descr *descr_test __read_mostly;
Waiman Longd86998b2019-05-20 10:14:46 -040084static struct kmem_cache *obj_cache __read_mostly;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070085
Waiman Longc4b73aa2017-01-05 15:17:03 -050086/*
Waiman Long0cad93c2017-02-07 16:40:30 -050087 * Track numbers of kmem_cache_alloc()/free() calls done.
Waiman Longc4b73aa2017-01-05 15:17:03 -050088 */
Waiman Long0cad93c2017-02-07 16:40:30 -050089static int debug_objects_allocated;
Waiman Longc4b73aa2017-01-05 15:17:03 -050090static int debug_objects_freed;
91
Thomas Gleixner337fff82009-03-16 10:04:53 +010092static void free_obj_work(struct work_struct *work);
93static DECLARE_WORK(debug_obj_work, free_obj_work);
94
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070095static int __init enable_object_debug(char *str)
96{
97 debug_objects_enabled = 1;
98 return 0;
99}
Kyle McMartin3e8ebb52009-03-01 20:41:41 -0500100
101static int __init disable_object_debug(char *str)
102{
103 debug_objects_enabled = 0;
104 return 0;
105}
106
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700107early_param("debug_objects", enable_object_debug);
Kyle McMartin3e8ebb52009-03-01 20:41:41 -0500108early_param("no_debug_objects", disable_object_debug);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700109
110static const char *obj_states[ODEBUG_STATE_MAX] = {
111 [ODEBUG_STATE_NONE] = "none",
112 [ODEBUG_STATE_INIT] = "initialized",
113 [ODEBUG_STATE_INACTIVE] = "inactive",
114 [ODEBUG_STATE_ACTIVE] = "active",
115 [ODEBUG_STATE_DESTROYED] = "destroyed",
116 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
117};
118
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200119static void fill_pool(void)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700120{
121 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
Yang Shi36c4ead2018-02-06 07:18:26 +0800122 struct debug_obj *new, *obj;
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200123 unsigned long flags;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700124
Waiman Long97dd5522017-01-05 15:17:04 -0500125 if (likely(obj_pool_free >= debug_objects_pool_min_level))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200126 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700127
Yang Shi36c4ead2018-02-06 07:18:26 +0800128 /*
129 * Reuse objs from the global free list; they will be reinitialized
130 * when allocating.
131 */
132 while (obj_nr_tofree && (obj_pool_free < obj_pool_min_free)) {
133 raw_spin_lock_irqsave(&pool_lock, flags);
134 /*
135 * Recheck with the lock held as the worker thread might have
136 * won the race and freed the global free list already.
137 */
138 if (obj_nr_tofree) {
139 obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
140 hlist_del(&obj->node);
141 obj_nr_tofree--;
142 hlist_add_head(&obj->node, &obj_pool);
143 obj_pool_free++;
144 }
145 raw_spin_unlock_irqrestore(&pool_lock, flags);
146 }
147
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700148 if (unlikely(!obj_cache))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200149 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700150
Waiman Long97dd5522017-01-05 15:17:04 -0500151 while (obj_pool_free < debug_objects_pool_min_level) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700152
153 new = kmem_cache_zalloc(obj_cache, gfp);
154 if (!new)
Dan Carpenter33408082012-04-18 14:28:10 +0300155 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700156
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100157 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700158 hlist_add_head(&new->node, &obj_pool);
Waiman Long0cad93c2017-02-07 16:40:30 -0500159 debug_objects_allocated++;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700160 obj_pool_free++;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100161 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700162 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700163}
164
165/*
166 * Lookup an object in the hash bucket.
167 */
168static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
169{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700170 struct debug_obj *obj;
171 int cnt = 0;
172
Sasha Levinb67bfe02013-02-27 17:06:00 -0800173 hlist_for_each_entry(obj, &b->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700174 cnt++;
175 if (obj->object == addr)
176 return obj;
177 }
178 if (cnt > debug_objects_maxchain)
179 debug_objects_maxchain = cnt;
180
181 return NULL;
182}
183
184/*
Waiman Longd86998b2019-05-20 10:14:46 -0400185 * Allocate a new object from the hlist
186 */
187static struct debug_obj *__alloc_object(struct hlist_head *list)
188{
189 struct debug_obj *obj = NULL;
190
191 if (list->first) {
192 obj = hlist_entry(list->first, typeof(*obj), node);
193 hlist_del(&obj->node);
194 }
195
196 return obj;
197}
198
199/*
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200200 * Allocate a new object. If the pool is empty, switch off the debugger.
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200201 * Must be called with interrupts disabled.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700202 */
203static struct debug_obj *
204alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
205{
Waiman Longd86998b2019-05-20 10:14:46 -0400206 struct debug_percpu_free *percpu_pool;
207 struct debug_obj *obj;
208
209 if (likely(obj_cache)) {
210 percpu_pool = this_cpu_ptr(&percpu_obj_pool);
211 obj = __alloc_object(&percpu_pool->free_objs);
212 if (obj) {
213 percpu_pool->obj_free--;
214 goto init_obj;
215 }
216 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700217
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100218 raw_spin_lock(&pool_lock);
Waiman Longd86998b2019-05-20 10:14:46 -0400219 obj = __alloc_object(&obj_pool);
220 if (obj) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700221 obj_pool_used++;
222 if (obj_pool_used > obj_pool_max_used)
223 obj_pool_max_used = obj_pool_used;
224
225 obj_pool_free--;
226 if (obj_pool_free < obj_pool_min_free)
227 obj_pool_min_free = obj_pool_free;
228 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100229 raw_spin_unlock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700230
Waiman Longd86998b2019-05-20 10:14:46 -0400231init_obj:
232 if (obj) {
233 obj->object = addr;
234 obj->descr = descr;
235 obj->state = ODEBUG_STATE_NONE;
236 obj->astate = 0;
237 hlist_add_head(&obj->node, &b->list);
238 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700239 return obj;
240}
241
242/*
Thomas Gleixner337fff82009-03-16 10:04:53 +0100243 * workqueue function to free objects.
Waiman Long858274b2017-01-05 15:17:05 -0500244 *
245 * To reduce contention on the global pool_lock, the actual freeing of
Yang Shi636e1972018-02-06 07:18:27 +0800246 * debug objects will be delayed if the pool_lock is busy.
Thomas Gleixner337fff82009-03-16 10:04:53 +0100247 */
248static void free_obj_work(struct work_struct *work)
249{
Yang Shi36c4ead2018-02-06 07:18:26 +0800250 struct hlist_node *tmp;
251 struct debug_obj *obj;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100252 unsigned long flags;
Yang Shi36c4ead2018-02-06 07:18:26 +0800253 HLIST_HEAD(tofree);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100254
Waiman Long858274b2017-01-05 15:17:05 -0500255 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
256 return;
Yang Shi36c4ead2018-02-06 07:18:26 +0800257
258 /*
259 * The objs on the pool list might be allocated before the work is
260 * run, so recheck if pool list it full or not, if not fill pool
261 * list from the global free list
262 */
263 while (obj_nr_tofree && obj_pool_free < debug_objects_pool_size) {
264 obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
265 hlist_del(&obj->node);
266 hlist_add_head(&obj->node, &obj_pool);
267 obj_pool_free++;
268 obj_nr_tofree--;
269 }
270
271 /*
272 * Pool list is already full and there are still objs on the free
273 * list. Move remaining free objs to a temporary list to free the
274 * memory outside the pool_lock held region.
275 */
276 if (obj_nr_tofree) {
277 hlist_move_list(&obj_to_free, &tofree);
Arnd Bergmann04148182018-02-22 16:52:58 +0100278 debug_objects_freed += obj_nr_tofree;
Yang Shi36c4ead2018-02-06 07:18:26 +0800279 obj_nr_tofree = 0;
280 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100281 raw_spin_unlock_irqrestore(&pool_lock, flags);
Yang Shi36c4ead2018-02-06 07:18:26 +0800282
283 hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
284 hlist_del(&obj->node);
285 kmem_cache_free(obj_cache, obj);
286 }
Thomas Gleixner337fff82009-03-16 10:04:53 +0100287}
288
Yang Shi636e1972018-02-06 07:18:27 +0800289static bool __free_object(struct debug_obj *obj)
290{
291 unsigned long flags;
292 bool work;
Waiman Longd86998b2019-05-20 10:14:46 -0400293 struct debug_percpu_free *percpu_pool;
Yang Shi636e1972018-02-06 07:18:27 +0800294
Waiman Longd86998b2019-05-20 10:14:46 -0400295 local_irq_save(flags);
296 /*
297 * Try to free it into the percpu pool first.
298 */
299 percpu_pool = this_cpu_ptr(&percpu_obj_pool);
300 if (obj_cache && percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) {
301 hlist_add_head(&obj->node, &percpu_pool->free_objs);
302 percpu_pool->obj_free++;
303 local_irq_restore(flags);
304 return false;
305 }
306
307 raw_spin_lock(&pool_lock);
Yang Shi636e1972018-02-06 07:18:27 +0800308 work = (obj_pool_free > debug_objects_pool_size) && obj_cache;
309 obj_pool_used--;
310
311 if (work) {
312 obj_nr_tofree++;
313 hlist_add_head(&obj->node, &obj_to_free);
314 } else {
315 obj_pool_free++;
316 hlist_add_head(&obj->node, &obj_pool);
317 }
Waiman Longd86998b2019-05-20 10:14:46 -0400318 raw_spin_unlock(&pool_lock);
319 local_irq_restore(flags);
Yang Shi636e1972018-02-06 07:18:27 +0800320 return work;
321}
322
Thomas Gleixner337fff82009-03-16 10:04:53 +0100323/*
324 * Put the object back into the pool and schedule work to free objects
325 * if necessary.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700326 */
327static void free_object(struct debug_obj *obj)
328{
Yang Shi636e1972018-02-06 07:18:27 +0800329 if (__free_object(obj))
Thomas Gleixner337fff82009-03-16 10:04:53 +0100330 schedule_work(&debug_obj_work);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700331}
332
333/*
334 * We run out of memory. That means we probably have tons of objects
335 * allocated.
336 */
337static void debug_objects_oom(void)
338{
339 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800340 struct hlist_node *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200341 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700342 struct debug_obj *obj;
343 unsigned long flags;
344 int i;
345
Fabian Frederick719e4842014-06-04 16:06:04 -0700346 pr_warn("Out of memory. ODEBUG disabled\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700347
348 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100349 raw_spin_lock_irqsave(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200350 hlist_move_list(&db->list, &freelist);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100351 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200352
353 /* Now free them */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800354 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700355 hlist_del(&obj->node);
356 free_object(obj);
357 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700358 }
359}
360
361/*
362 * We use the pfn of the address for the hash. That way we can check
363 * for freed objects simply by checking the affected bucket.
364 */
365static struct debug_bucket *get_bucket(unsigned long addr)
366{
367 unsigned long hash;
368
369 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
370 return &obj_hash[hash];
371}
372
373static void debug_print_object(struct debug_obj *obj, char *msg)
374{
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100375 struct debug_obj_descr *descr = obj->descr;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700376 static int limit;
377
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100378 if (limit < 5 && descr != descr_test) {
379 void *hint = descr->debug_hint ?
380 descr->debug_hint(obj->object) : NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700381 limit++;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400382 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100383 "object type: %s hint: %pS\n",
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400384 msg, obj_states[obj->state], obj->astate,
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100385 descr->name, hint);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700386 }
387 debug_objects_warnings++;
388}
389
390/*
391 * Try to repair the damage, so we have a better chance to get useful
392 * debug output.
393 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700394static bool
395debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700396 void * addr, enum debug_obj_state state)
397{
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700398 if (fixup && fixup(addr, state)) {
399 debug_objects_fixups++;
400 return true;
401 }
402 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700403}
404
405static void debug_object_is_on_stack(void *addr, int onstack)
406{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700407 int is_on_stack;
408 static int limit;
409
410 if (limit > 4)
411 return;
412
FUJITA Tomonori8b05c7e2008-07-23 21:26:53 -0700413 is_on_stack = object_is_on_stack(addr);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700414 if (is_on_stack == onstack)
415 return;
416
417 limit++;
418 if (is_on_stack)
Joel Fernandes (Google)fc91a3c2018-07-23 14:25:31 -0700419 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
420 task_stack_page(current));
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700421 else
Joel Fernandes (Google)fc91a3c2018-07-23 14:25:31 -0700422 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
423 task_stack_page(current));
424
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700425 WARN_ON(1);
426}
427
428static void
429__debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
430{
431 enum debug_obj_state state;
432 struct debug_bucket *db;
433 struct debug_obj *obj;
434 unsigned long flags;
435
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200436 fill_pool();
437
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700438 db = get_bucket((unsigned long) addr);
439
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100440 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700441
442 obj = lookup_object(addr, db);
443 if (!obj) {
444 obj = alloc_object(addr, db, descr);
445 if (!obj) {
446 debug_objects_enabled = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100447 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700448 debug_objects_oom();
449 return;
450 }
451 debug_object_is_on_stack(addr, onstack);
452 }
453
454 switch (obj->state) {
455 case ODEBUG_STATE_NONE:
456 case ODEBUG_STATE_INIT:
457 case ODEBUG_STATE_INACTIVE:
458 obj->state = ODEBUG_STATE_INIT;
459 break;
460
461 case ODEBUG_STATE_ACTIVE:
462 debug_print_object(obj, "init");
463 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100464 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700465 debug_object_fixup(descr->fixup_init, addr, state);
466 return;
467
468 case ODEBUG_STATE_DESTROYED:
469 debug_print_object(obj, "init");
470 break;
471 default:
472 break;
473 }
474
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100475 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700476}
477
478/**
479 * debug_object_init - debug checks when an object is initialized
480 * @addr: address of the object
481 * @descr: pointer to an object specific debug description structure
482 */
483void debug_object_init(void *addr, struct debug_obj_descr *descr)
484{
485 if (!debug_objects_enabled)
486 return;
487
488 __debug_object_init(addr, descr, 0);
489}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800490EXPORT_SYMBOL_GPL(debug_object_init);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700491
492/**
493 * debug_object_init_on_stack - debug checks when an object on stack is
494 * initialized
495 * @addr: address of the object
496 * @descr: pointer to an object specific debug description structure
497 */
498void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
499{
500 if (!debug_objects_enabled)
501 return;
502
503 __debug_object_init(addr, descr, 1);
504}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800505EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700506
507/**
508 * debug_object_activate - debug checks when an object is activated
509 * @addr: address of the object
510 * @descr: pointer to an object specific debug description structure
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700511 * Returns 0 for success, -EINVAL for check failed.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700512 */
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700513int debug_object_activate(void *addr, struct debug_obj_descr *descr)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700514{
515 enum debug_obj_state state;
516 struct debug_bucket *db;
517 struct debug_obj *obj;
518 unsigned long flags;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700519 int ret;
Stephen Boydfeac18d2011-11-07 19:48:26 -0800520 struct debug_obj o = { .object = addr,
521 .state = ODEBUG_STATE_NOTAVAILABLE,
522 .descr = descr };
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700523
524 if (!debug_objects_enabled)
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700525 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700526
527 db = get_bucket((unsigned long) addr);
528
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100529 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700530
531 obj = lookup_object(addr, db);
532 if (obj) {
533 switch (obj->state) {
534 case ODEBUG_STATE_INIT:
535 case ODEBUG_STATE_INACTIVE:
536 obj->state = ODEBUG_STATE_ACTIVE;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700537 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700538 break;
539
540 case ODEBUG_STATE_ACTIVE:
541 debug_print_object(obj, "activate");
542 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100543 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700544 ret = debug_object_fixup(descr->fixup_activate, addr, state);
Du, Changbine7a8e782016-05-19 17:09:23 -0700545 return ret ? 0 : -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700546
547 case ODEBUG_STATE_DESTROYED:
548 debug_print_object(obj, "activate");
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700549 ret = -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700550 break;
551 default:
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700552 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700553 break;
554 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100555 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700556 return ret;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700557 }
558
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100559 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700560 /*
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700561 * We are here when a static object is activated. We
562 * let the type specific code confirm whether this is
563 * true or not. if true, we just make sure that the
564 * static object is tracked in the object tracker. If
565 * not, this must be a bug, so we try to fix it up.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700566 */
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700567 if (descr->is_static_object && descr->is_static_object(addr)) {
568 /* track this static object */
569 debug_object_init(addr, descr);
570 debug_object_activate(addr, descr);
571 } else {
Stephen Boydfeac18d2011-11-07 19:48:26 -0800572 debug_print_object(&o, "activate");
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700573 ret = debug_object_fixup(descr->fixup_activate, addr,
574 ODEBUG_STATE_NOTAVAILABLE);
575 return ret ? 0 : -EINVAL;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700576 }
577 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700578}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800579EXPORT_SYMBOL_GPL(debug_object_activate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700580
581/**
582 * debug_object_deactivate - debug checks when an object is deactivated
583 * @addr: address of the object
584 * @descr: pointer to an object specific debug description structure
585 */
586void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
587{
588 struct debug_bucket *db;
589 struct debug_obj *obj;
590 unsigned long flags;
591
592 if (!debug_objects_enabled)
593 return;
594
595 db = get_bucket((unsigned long) addr);
596
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100597 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700598
599 obj = lookup_object(addr, db);
600 if (obj) {
601 switch (obj->state) {
602 case ODEBUG_STATE_INIT:
603 case ODEBUG_STATE_INACTIVE:
604 case ODEBUG_STATE_ACTIVE:
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400605 if (!obj->astate)
606 obj->state = ODEBUG_STATE_INACTIVE;
607 else
608 debug_print_object(obj, "deactivate");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700609 break;
610
611 case ODEBUG_STATE_DESTROYED:
612 debug_print_object(obj, "deactivate");
613 break;
614 default:
615 break;
616 }
617 } else {
618 struct debug_obj o = { .object = addr,
619 .state = ODEBUG_STATE_NOTAVAILABLE,
620 .descr = descr };
621
622 debug_print_object(&o, "deactivate");
623 }
624
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100625 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700626}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800627EXPORT_SYMBOL_GPL(debug_object_deactivate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700628
629/**
630 * debug_object_destroy - debug checks when an object is destroyed
631 * @addr: address of the object
632 * @descr: pointer to an object specific debug description structure
633 */
634void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
635{
636 enum debug_obj_state state;
637 struct debug_bucket *db;
638 struct debug_obj *obj;
639 unsigned long flags;
640
641 if (!debug_objects_enabled)
642 return;
643
644 db = get_bucket((unsigned long) addr);
645
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100646 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700647
648 obj = lookup_object(addr, db);
649 if (!obj)
650 goto out_unlock;
651
652 switch (obj->state) {
653 case ODEBUG_STATE_NONE:
654 case ODEBUG_STATE_INIT:
655 case ODEBUG_STATE_INACTIVE:
656 obj->state = ODEBUG_STATE_DESTROYED;
657 break;
658 case ODEBUG_STATE_ACTIVE:
659 debug_print_object(obj, "destroy");
660 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100661 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700662 debug_object_fixup(descr->fixup_destroy, addr, state);
663 return;
664
665 case ODEBUG_STATE_DESTROYED:
666 debug_print_object(obj, "destroy");
667 break;
668 default:
669 break;
670 }
671out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100672 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700673}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800674EXPORT_SYMBOL_GPL(debug_object_destroy);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700675
676/**
677 * debug_object_free - debug checks when an object is freed
678 * @addr: address of the object
679 * @descr: pointer to an object specific debug description structure
680 */
681void debug_object_free(void *addr, struct debug_obj_descr *descr)
682{
683 enum debug_obj_state state;
684 struct debug_bucket *db;
685 struct debug_obj *obj;
686 unsigned long flags;
687
688 if (!debug_objects_enabled)
689 return;
690
691 db = get_bucket((unsigned long) addr);
692
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100693 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700694
695 obj = lookup_object(addr, db);
696 if (!obj)
697 goto out_unlock;
698
699 switch (obj->state) {
700 case ODEBUG_STATE_ACTIVE:
701 debug_print_object(obj, "free");
702 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100703 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700704 debug_object_fixup(descr->fixup_free, addr, state);
705 return;
706 default:
707 hlist_del(&obj->node);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100708 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700709 free_object(obj);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200710 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700711 }
712out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100713 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700714}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800715EXPORT_SYMBOL_GPL(debug_object_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700716
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400717/**
Christine Chanb84d4352011-11-07 19:48:27 -0800718 * debug_object_assert_init - debug checks when object should be init-ed
719 * @addr: address of the object
720 * @descr: pointer to an object specific debug description structure
721 */
722void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
723{
724 struct debug_bucket *db;
725 struct debug_obj *obj;
726 unsigned long flags;
727
728 if (!debug_objects_enabled)
729 return;
730
731 db = get_bucket((unsigned long) addr);
732
733 raw_spin_lock_irqsave(&db->lock, flags);
734
735 obj = lookup_object(addr, db);
736 if (!obj) {
737 struct debug_obj o = { .object = addr,
738 .state = ODEBUG_STATE_NOTAVAILABLE,
739 .descr = descr };
740
741 raw_spin_unlock_irqrestore(&db->lock, flags);
742 /*
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700743 * Maybe the object is static, and we let the type specific
744 * code confirm. Track this static object if true, else invoke
745 * fixup.
Christine Chanb84d4352011-11-07 19:48:27 -0800746 */
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700747 if (descr->is_static_object && descr->is_static_object(addr)) {
748 /* Track this static object */
749 debug_object_init(addr, descr);
750 } else {
Christine Chanb84d4352011-11-07 19:48:27 -0800751 debug_print_object(&o, "assert_init");
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700752 debug_object_fixup(descr->fixup_assert_init, addr,
753 ODEBUG_STATE_NOTAVAILABLE);
754 }
Christine Chanb84d4352011-11-07 19:48:27 -0800755 return;
756 }
757
758 raw_spin_unlock_irqrestore(&db->lock, flags);
759}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800760EXPORT_SYMBOL_GPL(debug_object_assert_init);
Christine Chanb84d4352011-11-07 19:48:27 -0800761
762/**
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400763 * debug_object_active_state - debug checks object usage state machine
764 * @addr: address of the object
765 * @descr: pointer to an object specific debug description structure
766 * @expect: expected state
767 * @next: state to move to if expected state is found
768 */
769void
770debug_object_active_state(void *addr, struct debug_obj_descr *descr,
771 unsigned int expect, unsigned int next)
772{
773 struct debug_bucket *db;
774 struct debug_obj *obj;
775 unsigned long flags;
776
777 if (!debug_objects_enabled)
778 return;
779
780 db = get_bucket((unsigned long) addr);
781
782 raw_spin_lock_irqsave(&db->lock, flags);
783
784 obj = lookup_object(addr, db);
785 if (obj) {
786 switch (obj->state) {
787 case ODEBUG_STATE_ACTIVE:
788 if (obj->astate == expect)
789 obj->astate = next;
790 else
791 debug_print_object(obj, "active_state");
792 break;
793
794 default:
795 debug_print_object(obj, "active_state");
796 break;
797 }
798 } else {
799 struct debug_obj o = { .object = addr,
800 .state = ODEBUG_STATE_NOTAVAILABLE,
801 .descr = descr };
802
803 debug_print_object(&o, "active_state");
804 }
805
806 raw_spin_unlock_irqrestore(&db->lock, flags);
807}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800808EXPORT_SYMBOL_GPL(debug_object_active_state);
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400809
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700810#ifdef CONFIG_DEBUG_OBJECTS_FREE
811static void __debug_check_no_obj_freed(const void *address, unsigned long size)
812{
813 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700814 struct debug_obj_descr *descr;
815 enum debug_obj_state state;
816 struct debug_bucket *db;
Yang Shi1ea9b982018-02-06 07:18:28 +0800817 struct hlist_node *tmp;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700818 struct debug_obj *obj;
Yang Shibd9dcd02018-02-06 07:18:25 +0800819 int cnt, objs_checked = 0;
Yang Shi1ea9b982018-02-06 07:18:28 +0800820 bool work = false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700821
822 saddr = (unsigned long) address;
823 eaddr = saddr + size;
824 paddr = saddr & ODEBUG_CHUNK_MASK;
825 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
826 chunks >>= ODEBUG_CHUNK_SHIFT;
827
828 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
829 db = get_bucket(paddr);
830
831repeat:
832 cnt = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100833 raw_spin_lock_irqsave(&db->lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800834 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700835 cnt++;
836 oaddr = (unsigned long) obj->object;
837 if (oaddr < saddr || oaddr >= eaddr)
838 continue;
839
840 switch (obj->state) {
841 case ODEBUG_STATE_ACTIVE:
842 debug_print_object(obj, "free");
843 descr = obj->descr;
844 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100845 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700846 debug_object_fixup(descr->fixup_free,
847 (void *) oaddr, state);
848 goto repeat;
849 default:
850 hlist_del(&obj->node);
Yang Shi1ea9b982018-02-06 07:18:28 +0800851 work |= __free_object(obj);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700852 break;
853 }
854 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100855 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200856
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700857 if (cnt > debug_objects_maxchain)
858 debug_objects_maxchain = cnt;
Yang Shibd9dcd02018-02-06 07:18:25 +0800859
860 objs_checked += cnt;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700861 }
Yang Shibd9dcd02018-02-06 07:18:25 +0800862
863 if (objs_checked > debug_objects_maxchecked)
864 debug_objects_maxchecked = objs_checked;
Yang Shi1ea9b982018-02-06 07:18:28 +0800865
866 /* Schedule work to actually kmem_cache_free() objects */
867 if (work)
868 schedule_work(&debug_obj_work);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700869}
870
871void debug_check_no_obj_freed(const void *address, unsigned long size)
872{
873 if (debug_objects_enabled)
874 __debug_check_no_obj_freed(address, size);
875}
876#endif
877
878#ifdef CONFIG_DEBUG_FS
879
880static int debug_stats_show(struct seq_file *m, void *v)
881{
Waiman Longd86998b2019-05-20 10:14:46 -0400882 int cpu, obj_percpu_free = 0;
883
884 for_each_possible_cpu(cpu)
885 obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu);
886
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700887 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
Yang Shibd9dcd02018-02-06 07:18:25 +0800888 seq_printf(m, "max_checked :%d\n", debug_objects_maxchecked);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700889 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
890 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
Waiman Longd86998b2019-05-20 10:14:46 -0400891 seq_printf(m, "pool_free :%d\n", obj_pool_free + obj_percpu_free);
892 seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700893 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
Waiman Longd86998b2019-05-20 10:14:46 -0400894 seq_printf(m, "pool_used :%d\n", obj_pool_used - obj_percpu_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700895 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
Yang Shi36c4ead2018-02-06 07:18:26 +0800896 seq_printf(m, "on_free_list :%d\n", obj_nr_tofree);
Waiman Long0cad93c2017-02-07 16:40:30 -0500897 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
898 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700899 return 0;
900}
901
902static int debug_stats_open(struct inode *inode, struct file *filp)
903{
904 return single_open(filp, debug_stats_show, NULL);
905}
906
907static const struct file_operations debug_stats_fops = {
908 .open = debug_stats_open,
909 .read = seq_read,
910 .llseek = seq_lseek,
911 .release = single_release,
912};
913
914static int __init debug_objects_init_debugfs(void)
915{
Greg Kroah-Hartmanfecb0d92019-06-12 17:35:13 +0200916 struct dentry *dbgdir;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700917
918 if (!debug_objects_enabled)
919 return 0;
920
921 dbgdir = debugfs_create_dir("debug_objects", NULL);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700922
Greg Kroah-Hartmanfecb0d92019-06-12 17:35:13 +0200923 debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700924
925 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700926}
927__initcall(debug_objects_init_debugfs);
928
929#else
930static inline void debug_objects_init_debugfs(void) { }
931#endif
932
933#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
934
935/* Random data structure for the self test */
936struct self_test {
937 unsigned long dummy1[6];
938 int static_init;
939 unsigned long dummy2[3];
940};
941
942static __initdata struct debug_obj_descr descr_type_test;
943
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700944static bool __init is_static_object(void *addr)
945{
946 struct self_test *obj = addr;
947
948 return obj->static_init;
949}
950
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700951/*
952 * fixup_init is called when:
953 * - an active object is initialized
954 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700955static bool __init fixup_init(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700956{
957 struct self_test *obj = addr;
958
959 switch (state) {
960 case ODEBUG_STATE_ACTIVE:
961 debug_object_deactivate(obj, &descr_type_test);
962 debug_object_init(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700963 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700964 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700965 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700966 }
967}
968
969/*
970 * fixup_activate is called when:
971 * - an active object is activated
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700972 * - an unknown non-static object is activated
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700973 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700974static bool __init fixup_activate(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700975{
976 struct self_test *obj = addr;
977
978 switch (state) {
979 case ODEBUG_STATE_NOTAVAILABLE:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700980 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700981 case ODEBUG_STATE_ACTIVE:
982 debug_object_deactivate(obj, &descr_type_test);
983 debug_object_activate(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700984 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700985
986 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700987 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700988 }
989}
990
991/*
992 * fixup_destroy is called when:
993 * - an active object is destroyed
994 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700995static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700996{
997 struct self_test *obj = addr;
998
999 switch (state) {
1000 case ODEBUG_STATE_ACTIVE:
1001 debug_object_deactivate(obj, &descr_type_test);
1002 debug_object_destroy(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001003 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001004 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001005 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001006 }
1007}
1008
1009/*
1010 * fixup_free is called when:
1011 * - an active object is freed
1012 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001013static bool __init fixup_free(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001014{
1015 struct self_test *obj = addr;
1016
1017 switch (state) {
1018 case ODEBUG_STATE_ACTIVE:
1019 debug_object_deactivate(obj, &descr_type_test);
1020 debug_object_free(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001021 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001022 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001023 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001024 }
1025}
1026
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001027static int __init
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001028check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
1029{
1030 struct debug_bucket *db;
1031 struct debug_obj *obj;
1032 unsigned long flags;
1033 int res = -EINVAL;
1034
1035 db = get_bucket((unsigned long) addr);
1036
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001037 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001038
1039 obj = lookup_object(addr, db);
1040 if (!obj && state != ODEBUG_STATE_NONE) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001041 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001042 goto out;
1043 }
1044 if (obj && obj->state != state) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001045 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001046 obj->state, state);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001047 goto out;
1048 }
1049 if (fixups != debug_objects_fixups) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001050 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001051 fixups, debug_objects_fixups);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001052 goto out;
1053 }
1054 if (warnings != debug_objects_warnings) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001055 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001056 warnings, debug_objects_warnings);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001057 goto out;
1058 }
1059 res = 0;
1060out:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001061 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001062 if (res)
1063 debug_objects_enabled = 0;
1064 return res;
1065}
1066
1067static __initdata struct debug_obj_descr descr_type_test = {
1068 .name = "selftest",
Du, Changbinb9fdac7f2016-05-19 17:09:41 -07001069 .is_static_object = is_static_object,
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001070 .fixup_init = fixup_init,
1071 .fixup_activate = fixup_activate,
1072 .fixup_destroy = fixup_destroy,
1073 .fixup_free = fixup_free,
1074};
1075
1076static __initdata struct self_test obj = { .static_init = 0 };
1077
1078static void __init debug_objects_selftest(void)
1079{
1080 int fixups, oldfixups, warnings, oldwarnings;
1081 unsigned long flags;
1082
1083 local_irq_save(flags);
1084
1085 fixups = oldfixups = debug_objects_fixups;
1086 warnings = oldwarnings = debug_objects_warnings;
1087 descr_test = &descr_type_test;
1088
1089 debug_object_init(&obj, &descr_type_test);
1090 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1091 goto out;
1092 debug_object_activate(&obj, &descr_type_test);
1093 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1094 goto out;
1095 debug_object_activate(&obj, &descr_type_test);
1096 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1097 goto out;
1098 debug_object_deactivate(&obj, &descr_type_test);
1099 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1100 goto out;
1101 debug_object_destroy(&obj, &descr_type_test);
1102 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1103 goto out;
1104 debug_object_init(&obj, &descr_type_test);
1105 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1106 goto out;
1107 debug_object_activate(&obj, &descr_type_test);
1108 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1109 goto out;
1110 debug_object_deactivate(&obj, &descr_type_test);
1111 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1112 goto out;
1113 debug_object_free(&obj, &descr_type_test);
1114 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1115 goto out;
1116
1117 obj.static_init = 1;
1118 debug_object_activate(&obj, &descr_type_test);
Stephen Boyd9f78ff02012-03-05 14:59:17 -08001119 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001120 goto out;
1121 debug_object_init(&obj, &descr_type_test);
1122 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1123 goto out;
1124 debug_object_free(&obj, &descr_type_test);
1125 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1126 goto out;
1127
1128#ifdef CONFIG_DEBUG_OBJECTS_FREE
1129 debug_object_init(&obj, &descr_type_test);
1130 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1131 goto out;
1132 debug_object_activate(&obj, &descr_type_test);
1133 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1134 goto out;
1135 __debug_check_no_obj_freed(&obj, sizeof(obj));
1136 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1137 goto out;
1138#endif
Fabian Frederick719e4842014-06-04 16:06:04 -07001139 pr_info("selftest passed\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001140
1141out:
1142 debug_objects_fixups = oldfixups;
1143 debug_objects_warnings = oldwarnings;
1144 descr_test = NULL;
1145
1146 local_irq_restore(flags);
1147}
1148#else
1149static inline void debug_objects_selftest(void) { }
1150#endif
1151
1152/*
1153 * Called during early boot to initialize the hash buckets and link
1154 * the static object pool objects into the poll list. After this call
1155 * the object tracker is fully operational.
1156 */
1157void __init debug_objects_early_init(void)
1158{
1159 int i;
1160
1161 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001162 raw_spin_lock_init(&obj_hash[i].lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001163
1164 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1165 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1166}
1167
1168/*
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001169 * Convert the statically allocated objects to dynamic ones:
1170 */
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001171static int __init debug_objects_replace_static_objects(void)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001172{
1173 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001174 struct hlist_node *tmp;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001175 struct debug_obj *obj, *new;
1176 HLIST_HEAD(objects);
1177 int i, cnt = 0;
1178
1179 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1180 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1181 if (!obj)
1182 goto free;
1183 hlist_add_head(&obj->node, &objects);
1184 }
1185
1186 /*
Qian Caia9ee3a62018-12-28 00:32:32 -08001187 * debug_objects_mem_init() is now called early that only one CPU is up
1188 * and interrupts have been disabled, so it is safe to replace the
1189 * active object references.
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001190 */
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001191
1192 /* Remove the statically allocated objects from the pool */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001193 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001194 hlist_del(&obj->node);
1195 /* Move the allocated objects to the pool */
1196 hlist_move_list(&objects, &obj_pool);
1197
1198 /* Replace the active object references */
1199 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1200 hlist_move_list(&db->list, &objects);
1201
Sasha Levinb67bfe02013-02-27 17:06:00 -08001202 hlist_for_each_entry(obj, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001203 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1204 hlist_del(&new->node);
1205 /* copy object data */
1206 *new = *obj;
1207 hlist_add_head(&new->node, &db->list);
1208 cnt++;
1209 }
1210 }
1211
Fabian Frederickc0f35cc2014-06-04 16:06:05 -07001212 pr_debug("%d of %d active objects replaced\n",
1213 cnt, obj_pool_used);
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001214 return 0;
1215free:
Sasha Levinb67bfe02013-02-27 17:06:00 -08001216 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001217 hlist_del(&obj->node);
1218 kmem_cache_free(obj_cache, obj);
1219 }
1220 return -ENOMEM;
1221}
1222
1223/*
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001224 * Called after the kmem_caches are functional to setup a dedicated
1225 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1226 * prevents that the debug code is called on kmem_cache_free() for the
1227 * debug tracker objects to avoid recursive calls.
1228 */
1229void __init debug_objects_mem_init(void)
1230{
Waiman Longd86998b2019-05-20 10:14:46 -04001231 int cpu;
1232
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001233 if (!debug_objects_enabled)
1234 return;
1235
Waiman Longd86998b2019-05-20 10:14:46 -04001236 /*
1237 * Initialize the percpu object pools
1238 *
1239 * Initialization is not strictly necessary, but was done for
1240 * completeness.
1241 */
1242 for_each_possible_cpu(cpu)
1243 INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool.free_objs, cpu));
1244
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001245 obj_cache = kmem_cache_create("debug_objects_cache",
1246 sizeof (struct debug_obj), 0,
Qian Cai8de456c2018-11-30 14:09:48 -08001247 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
1248 NULL);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001249
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001250 if (!obj_cache || debug_objects_replace_static_objects()) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001251 debug_objects_enabled = 0;
Zhong Jiang3ff4f802018-08-01 00:24:58 +08001252 kmem_cache_destroy(obj_cache);
Fabian Frederick719e4842014-06-04 16:06:04 -07001253 pr_warn("out of memory.\n");
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001254 } else
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001255 debug_objects_selftest();
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001256}