blob: 714459a8dc108e94a4fd38dc80ee070c6b2a24f7 [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
Waiman Long634d61f2019-05-20 10:14:47 -040029#define ODEBUG_BATCH_SIZE 16
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070030
31#define ODEBUG_CHUNK_SHIFT PAGE_SHIFT
32#define ODEBUG_CHUNK_SIZE (1 << ODEBUG_CHUNK_SHIFT)
33#define ODEBUG_CHUNK_MASK (~(ODEBUG_CHUNK_SIZE - 1))
34
35struct debug_bucket {
36 struct hlist_head list;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010037 raw_spinlock_t lock;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070038};
39
Waiman Longd86998b2019-05-20 10:14:46 -040040/*
41 * Debug object percpu free list
42 * Access is protected by disabling irq
43 */
44struct debug_percpu_free {
45 struct hlist_head free_objs;
46 int obj_free;
47};
48
49static DEFINE_PER_CPU(struct debug_percpu_free, percpu_obj_pool);
50
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070051static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
52
Thomas Gleixner1be1cb72009-03-16 18:53:18 +010053static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070054
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010055static DEFINE_RAW_SPINLOCK(pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070056
57static HLIST_HEAD(obj_pool);
Yang Shi36c4ead2018-02-06 07:18:26 +080058static HLIST_HEAD(obj_to_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070059
Waiman Longd86998b2019-05-20 10:14:46 -040060/*
61 * Because of the presence of percpu free pools, obj_pool_free will
62 * under-count those in the percpu free pools. Similarly, obj_pool_used
63 * will over-count those in the percpu free pools. Adjustments will be
64 * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
65 * can be off.
66 */
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070067static int obj_pool_min_free = ODEBUG_POOL_SIZE;
68static int obj_pool_free = ODEBUG_POOL_SIZE;
69static int obj_pool_used;
70static int obj_pool_max_used;
Yang Shi36c4ead2018-02-06 07:18:26 +080071/* The number of objs on the global free list */
72static int obj_nr_tofree;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070073
74static int debug_objects_maxchain __read_mostly;
Arnd Bergmann163cf842018-03-13 14:18:46 +010075static int __maybe_unused debug_objects_maxchecked __read_mostly;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070076static int debug_objects_fixups __read_mostly;
77static int debug_objects_warnings __read_mostly;
Ingo Molnar3ae70202008-11-26 10:02:00 +010078static int debug_objects_enabled __read_mostly
79 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
Waiman Long97dd5522017-01-05 15:17:04 -050080static int debug_objects_pool_size __read_mostly
81 = ODEBUG_POOL_SIZE;
82static int debug_objects_pool_min_level __read_mostly
83 = ODEBUG_POOL_MIN_LEVEL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070084static struct debug_obj_descr *descr_test __read_mostly;
Waiman Longd86998b2019-05-20 10:14:46 -040085static struct kmem_cache *obj_cache __read_mostly;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070086
Waiman Longc4b73aa2017-01-05 15:17:03 -050087/*
Waiman Long0cad93c2017-02-07 16:40:30 -050088 * Track numbers of kmem_cache_alloc()/free() calls done.
Waiman Longc4b73aa2017-01-05 15:17:03 -050089 */
Waiman Long0cad93c2017-02-07 16:40:30 -050090static int debug_objects_allocated;
Waiman Longc4b73aa2017-01-05 15:17:03 -050091static int debug_objects_freed;
92
Thomas Gleixner337fff82009-03-16 10:04:53 +010093static void free_obj_work(struct work_struct *work);
94static DECLARE_WORK(debug_obj_work, free_obj_work);
95
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070096static int __init enable_object_debug(char *str)
97{
98 debug_objects_enabled = 1;
99 return 0;
100}
Kyle McMartin3e8ebb52009-03-01 20:41:41 -0500101
102static int __init disable_object_debug(char *str)
103{
104 debug_objects_enabled = 0;
105 return 0;
106}
107
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700108early_param("debug_objects", enable_object_debug);
Kyle McMartin3e8ebb52009-03-01 20:41:41 -0500109early_param("no_debug_objects", disable_object_debug);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700110
111static const char *obj_states[ODEBUG_STATE_MAX] = {
112 [ODEBUG_STATE_NONE] = "none",
113 [ODEBUG_STATE_INIT] = "initialized",
114 [ODEBUG_STATE_INACTIVE] = "inactive",
115 [ODEBUG_STATE_ACTIVE] = "active",
116 [ODEBUG_STATE_DESTROYED] = "destroyed",
117 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
118};
119
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200120static void fill_pool(void)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700121{
122 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
Yang Shi36c4ead2018-02-06 07:18:26 +0800123 struct debug_obj *new, *obj;
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200124 unsigned long flags;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700125
Waiman Long97dd5522017-01-05 15:17:04 -0500126 if (likely(obj_pool_free >= debug_objects_pool_min_level))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200127 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700128
Yang Shi36c4ead2018-02-06 07:18:26 +0800129 /*
130 * Reuse objs from the global free list; they will be reinitialized
131 * when allocating.
132 */
133 while (obj_nr_tofree && (obj_pool_free < obj_pool_min_free)) {
134 raw_spin_lock_irqsave(&pool_lock, flags);
135 /*
136 * Recheck with the lock held as the worker thread might have
137 * won the race and freed the global free list already.
138 */
139 if (obj_nr_tofree) {
140 obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
141 hlist_del(&obj->node);
142 obj_nr_tofree--;
143 hlist_add_head(&obj->node, &obj_pool);
144 obj_pool_free++;
145 }
146 raw_spin_unlock_irqrestore(&pool_lock, flags);
147 }
148
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700149 if (unlikely(!obj_cache))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200150 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700151
Waiman Long97dd5522017-01-05 15:17:04 -0500152 while (obj_pool_free < debug_objects_pool_min_level) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700153
154 new = kmem_cache_zalloc(obj_cache, gfp);
155 if (!new)
Dan Carpenter33408082012-04-18 14:28:10 +0300156 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700157
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100158 raw_spin_lock_irqsave(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700159 hlist_add_head(&new->node, &obj_pool);
Waiman Long0cad93c2017-02-07 16:40:30 -0500160 debug_objects_allocated++;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700161 obj_pool_free++;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100162 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700163 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700164}
165
166/*
167 * Lookup an object in the hash bucket.
168 */
169static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
170{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700171 struct debug_obj *obj;
172 int cnt = 0;
173
Sasha Levinb67bfe02013-02-27 17:06:00 -0800174 hlist_for_each_entry(obj, &b->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700175 cnt++;
176 if (obj->object == addr)
177 return obj;
178 }
179 if (cnt > debug_objects_maxchain)
180 debug_objects_maxchain = cnt;
181
182 return NULL;
183}
184
185/*
Waiman Longd86998b2019-05-20 10:14:46 -0400186 * Allocate a new object from the hlist
187 */
188static struct debug_obj *__alloc_object(struct hlist_head *list)
189{
190 struct debug_obj *obj = NULL;
191
192 if (list->first) {
193 obj = hlist_entry(list->first, typeof(*obj), node);
194 hlist_del(&obj->node);
195 }
196
197 return obj;
198}
199
200/*
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200201 * Allocate a new object. If the pool is empty, switch off the debugger.
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200202 * Must be called with interrupts disabled.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700203 */
204static struct debug_obj *
205alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
206{
Waiman Long634d61f2019-05-20 10:14:47 -0400207 struct debug_percpu_free *percpu_pool = this_cpu_ptr(&percpu_obj_pool);
Waiman Longd86998b2019-05-20 10:14:46 -0400208 struct debug_obj *obj;
209
210 if (likely(obj_cache)) {
Waiman Longd86998b2019-05-20 10:14:46 -0400211 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++;
Waiman Long634d61f2019-05-20 10:14:47 -0400222 obj_pool_free--;
223
224 /*
225 * Looking ahead, allocate one batch of debug objects and
226 * put them into the percpu free pool.
227 */
228 if (likely(obj_cache)) {
229 int i;
230
231 for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
232 struct debug_obj *obj2;
233
234 obj2 = __alloc_object(&obj_pool);
235 if (!obj2)
236 break;
237 hlist_add_head(&obj2->node,
238 &percpu_pool->free_objs);
239 percpu_pool->obj_free++;
240 obj_pool_used++;
241 obj_pool_free--;
242 }
243 }
244
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700245 if (obj_pool_used > obj_pool_max_used)
246 obj_pool_max_used = obj_pool_used;
247
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700248 if (obj_pool_free < obj_pool_min_free)
249 obj_pool_min_free = obj_pool_free;
250 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100251 raw_spin_unlock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700252
Waiman Longd86998b2019-05-20 10:14:46 -0400253init_obj:
254 if (obj) {
255 obj->object = addr;
256 obj->descr = descr;
257 obj->state = ODEBUG_STATE_NONE;
258 obj->astate = 0;
259 hlist_add_head(&obj->node, &b->list);
260 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700261 return obj;
262}
263
264/*
Thomas Gleixner337fff82009-03-16 10:04:53 +0100265 * workqueue function to free objects.
Waiman Long858274b2017-01-05 15:17:05 -0500266 *
267 * To reduce contention on the global pool_lock, the actual freeing of
Yang Shi636e1972018-02-06 07:18:27 +0800268 * debug objects will be delayed if the pool_lock is busy.
Thomas Gleixner337fff82009-03-16 10:04:53 +0100269 */
270static void free_obj_work(struct work_struct *work)
271{
Yang Shi36c4ead2018-02-06 07:18:26 +0800272 struct hlist_node *tmp;
273 struct debug_obj *obj;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100274 unsigned long flags;
Yang Shi36c4ead2018-02-06 07:18:26 +0800275 HLIST_HEAD(tofree);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100276
Waiman Long858274b2017-01-05 15:17:05 -0500277 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
278 return;
Yang Shi36c4ead2018-02-06 07:18:26 +0800279
280 /*
281 * The objs on the pool list might be allocated before the work is
282 * run, so recheck if pool list it full or not, if not fill pool
283 * list from the global free list
284 */
285 while (obj_nr_tofree && obj_pool_free < debug_objects_pool_size) {
286 obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
287 hlist_del(&obj->node);
288 hlist_add_head(&obj->node, &obj_pool);
289 obj_pool_free++;
290 obj_nr_tofree--;
291 }
292
293 /*
294 * Pool list is already full and there are still objs on the free
295 * list. Move remaining free objs to a temporary list to free the
296 * memory outside the pool_lock held region.
297 */
298 if (obj_nr_tofree) {
299 hlist_move_list(&obj_to_free, &tofree);
Arnd Bergmann04148182018-02-22 16:52:58 +0100300 debug_objects_freed += obj_nr_tofree;
Yang Shi36c4ead2018-02-06 07:18:26 +0800301 obj_nr_tofree = 0;
302 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100303 raw_spin_unlock_irqrestore(&pool_lock, flags);
Yang Shi36c4ead2018-02-06 07:18:26 +0800304
305 hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
306 hlist_del(&obj->node);
307 kmem_cache_free(obj_cache, obj);
308 }
Thomas Gleixner337fff82009-03-16 10:04:53 +0100309}
310
Yang Shi636e1972018-02-06 07:18:27 +0800311static bool __free_object(struct debug_obj *obj)
312{
Waiman Long634d61f2019-05-20 10:14:47 -0400313 struct debug_obj *objs[ODEBUG_BATCH_SIZE];
314 struct debug_percpu_free *percpu_pool;
315 int lookahead_count = 0;
Yang Shi636e1972018-02-06 07:18:27 +0800316 unsigned long flags;
317 bool work;
318
Waiman Longd86998b2019-05-20 10:14:46 -0400319 local_irq_save(flags);
Waiman Long634d61f2019-05-20 10:14:47 -0400320 if (!obj_cache)
321 goto free_to_obj_pool;
322
Waiman Longd86998b2019-05-20 10:14:46 -0400323 /*
324 * Try to free it into the percpu pool first.
325 */
326 percpu_pool = this_cpu_ptr(&percpu_obj_pool);
Waiman Long634d61f2019-05-20 10:14:47 -0400327 if (percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) {
Waiman Longd86998b2019-05-20 10:14:46 -0400328 hlist_add_head(&obj->node, &percpu_pool->free_objs);
329 percpu_pool->obj_free++;
330 local_irq_restore(flags);
331 return false;
332 }
333
Waiman Long634d61f2019-05-20 10:14:47 -0400334 /*
335 * As the percpu pool is full, look ahead and pull out a batch
336 * of objects from the percpu pool and free them as well.
337 */
338 for (; lookahead_count < ODEBUG_BATCH_SIZE; lookahead_count++) {
339 objs[lookahead_count] = __alloc_object(&percpu_pool->free_objs);
340 if (!objs[lookahead_count])
341 break;
342 percpu_pool->obj_free--;
343 }
344
345free_to_obj_pool:
Waiman Longd86998b2019-05-20 10:14:46 -0400346 raw_spin_lock(&pool_lock);
Yang Shi636e1972018-02-06 07:18:27 +0800347 work = (obj_pool_free > debug_objects_pool_size) && obj_cache;
348 obj_pool_used--;
349
350 if (work) {
351 obj_nr_tofree++;
352 hlist_add_head(&obj->node, &obj_to_free);
Waiman Long634d61f2019-05-20 10:14:47 -0400353 if (lookahead_count) {
354 obj_nr_tofree += lookahead_count;
355 obj_pool_used -= lookahead_count;
356 while (lookahead_count) {
357 hlist_add_head(&objs[--lookahead_count]->node,
358 &obj_to_free);
359 }
360 }
Yang Shi636e1972018-02-06 07:18:27 +0800361 } else {
362 obj_pool_free++;
363 hlist_add_head(&obj->node, &obj_pool);
Waiman Long634d61f2019-05-20 10:14:47 -0400364 if (lookahead_count) {
365 obj_pool_free += lookahead_count;
366 obj_pool_used -= lookahead_count;
367 while (lookahead_count) {
368 hlist_add_head(&objs[--lookahead_count]->node,
369 &obj_pool);
370 }
371 }
Yang Shi636e1972018-02-06 07:18:27 +0800372 }
Waiman Longd86998b2019-05-20 10:14:46 -0400373 raw_spin_unlock(&pool_lock);
374 local_irq_restore(flags);
Yang Shi636e1972018-02-06 07:18:27 +0800375 return work;
376}
377
Thomas Gleixner337fff82009-03-16 10:04:53 +0100378/*
379 * Put the object back into the pool and schedule work to free objects
380 * if necessary.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700381 */
382static void free_object(struct debug_obj *obj)
383{
Yang Shi636e1972018-02-06 07:18:27 +0800384 if (__free_object(obj))
Thomas Gleixner337fff82009-03-16 10:04:53 +0100385 schedule_work(&debug_obj_work);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700386}
387
388/*
389 * We run out of memory. That means we probably have tons of objects
390 * allocated.
391 */
392static void debug_objects_oom(void)
393{
394 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800395 struct hlist_node *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200396 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700397 struct debug_obj *obj;
398 unsigned long flags;
399 int i;
400
Fabian Frederick719e4842014-06-04 16:06:04 -0700401 pr_warn("Out of memory. ODEBUG disabled\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700402
403 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100404 raw_spin_lock_irqsave(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200405 hlist_move_list(&db->list, &freelist);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100406 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200407
408 /* Now free them */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800409 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700410 hlist_del(&obj->node);
411 free_object(obj);
412 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700413 }
414}
415
416/*
417 * We use the pfn of the address for the hash. That way we can check
418 * for freed objects simply by checking the affected bucket.
419 */
420static struct debug_bucket *get_bucket(unsigned long addr)
421{
422 unsigned long hash;
423
424 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
425 return &obj_hash[hash];
426}
427
428static void debug_print_object(struct debug_obj *obj, char *msg)
429{
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100430 struct debug_obj_descr *descr = obj->descr;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700431 static int limit;
432
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100433 if (limit < 5 && descr != descr_test) {
434 void *hint = descr->debug_hint ?
435 descr->debug_hint(obj->object) : NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700436 limit++;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400437 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100438 "object type: %s hint: %pS\n",
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400439 msg, obj_states[obj->state], obj->astate,
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100440 descr->name, hint);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700441 }
442 debug_objects_warnings++;
443}
444
445/*
446 * Try to repair the damage, so we have a better chance to get useful
447 * debug output.
448 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700449static bool
450debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700451 void * addr, enum debug_obj_state state)
452{
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700453 if (fixup && fixup(addr, state)) {
454 debug_objects_fixups++;
455 return true;
456 }
457 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700458}
459
460static void debug_object_is_on_stack(void *addr, int onstack)
461{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700462 int is_on_stack;
463 static int limit;
464
465 if (limit > 4)
466 return;
467
FUJITA Tomonori8b05c7e2008-07-23 21:26:53 -0700468 is_on_stack = object_is_on_stack(addr);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700469 if (is_on_stack == onstack)
470 return;
471
472 limit++;
473 if (is_on_stack)
Joel Fernandes (Google)fc91a3c2018-07-23 14:25:31 -0700474 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
475 task_stack_page(current));
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700476 else
Joel Fernandes (Google)fc91a3c2018-07-23 14:25:31 -0700477 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
478 task_stack_page(current));
479
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700480 WARN_ON(1);
481}
482
483static void
484__debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
485{
486 enum debug_obj_state state;
487 struct debug_bucket *db;
488 struct debug_obj *obj;
489 unsigned long flags;
490
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200491 fill_pool();
492
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700493 db = get_bucket((unsigned long) addr);
494
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100495 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700496
497 obj = lookup_object(addr, db);
498 if (!obj) {
499 obj = alloc_object(addr, db, descr);
500 if (!obj) {
501 debug_objects_enabled = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100502 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700503 debug_objects_oom();
504 return;
505 }
506 debug_object_is_on_stack(addr, onstack);
507 }
508
509 switch (obj->state) {
510 case ODEBUG_STATE_NONE:
511 case ODEBUG_STATE_INIT:
512 case ODEBUG_STATE_INACTIVE:
513 obj->state = ODEBUG_STATE_INIT;
514 break;
515
516 case ODEBUG_STATE_ACTIVE:
517 debug_print_object(obj, "init");
518 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100519 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700520 debug_object_fixup(descr->fixup_init, addr, state);
521 return;
522
523 case ODEBUG_STATE_DESTROYED:
524 debug_print_object(obj, "init");
525 break;
526 default:
527 break;
528 }
529
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100530 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700531}
532
533/**
534 * debug_object_init - debug checks when an object is initialized
535 * @addr: address of the object
536 * @descr: pointer to an object specific debug description structure
537 */
538void debug_object_init(void *addr, struct debug_obj_descr *descr)
539{
540 if (!debug_objects_enabled)
541 return;
542
543 __debug_object_init(addr, descr, 0);
544}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800545EXPORT_SYMBOL_GPL(debug_object_init);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700546
547/**
548 * debug_object_init_on_stack - debug checks when an object on stack is
549 * initialized
550 * @addr: address of the object
551 * @descr: pointer to an object specific debug description structure
552 */
553void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
554{
555 if (!debug_objects_enabled)
556 return;
557
558 __debug_object_init(addr, descr, 1);
559}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800560EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700561
562/**
563 * debug_object_activate - debug checks when an object is activated
564 * @addr: address of the object
565 * @descr: pointer to an object specific debug description structure
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700566 * Returns 0 for success, -EINVAL for check failed.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700567 */
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700568int debug_object_activate(void *addr, struct debug_obj_descr *descr)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700569{
570 enum debug_obj_state state;
571 struct debug_bucket *db;
572 struct debug_obj *obj;
573 unsigned long flags;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700574 int ret;
Stephen Boydfeac18d2011-11-07 19:48:26 -0800575 struct debug_obj o = { .object = addr,
576 .state = ODEBUG_STATE_NOTAVAILABLE,
577 .descr = descr };
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700578
579 if (!debug_objects_enabled)
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700580 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700581
582 db = get_bucket((unsigned long) addr);
583
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100584 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700585
586 obj = lookup_object(addr, db);
587 if (obj) {
588 switch (obj->state) {
589 case ODEBUG_STATE_INIT:
590 case ODEBUG_STATE_INACTIVE:
591 obj->state = ODEBUG_STATE_ACTIVE;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700592 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700593 break;
594
595 case ODEBUG_STATE_ACTIVE:
596 debug_print_object(obj, "activate");
597 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100598 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700599 ret = debug_object_fixup(descr->fixup_activate, addr, state);
Du, Changbine7a8e782016-05-19 17:09:23 -0700600 return ret ? 0 : -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700601
602 case ODEBUG_STATE_DESTROYED:
603 debug_print_object(obj, "activate");
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700604 ret = -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700605 break;
606 default:
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700607 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700608 break;
609 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100610 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700611 return ret;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700612 }
613
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100614 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700615 /*
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700616 * We are here when a static object is activated. We
617 * let the type specific code confirm whether this is
618 * true or not. if true, we just make sure that the
619 * static object is tracked in the object tracker. If
620 * not, this must be a bug, so we try to fix it up.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700621 */
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700622 if (descr->is_static_object && descr->is_static_object(addr)) {
623 /* track this static object */
624 debug_object_init(addr, descr);
625 debug_object_activate(addr, descr);
626 } else {
Stephen Boydfeac18d2011-11-07 19:48:26 -0800627 debug_print_object(&o, "activate");
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700628 ret = debug_object_fixup(descr->fixup_activate, addr,
629 ODEBUG_STATE_NOTAVAILABLE);
630 return ret ? 0 : -EINVAL;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700631 }
632 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700633}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800634EXPORT_SYMBOL_GPL(debug_object_activate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700635
636/**
637 * debug_object_deactivate - debug checks when an object is deactivated
638 * @addr: address of the object
639 * @descr: pointer to an object specific debug description structure
640 */
641void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
642{
643 struct debug_bucket *db;
644 struct debug_obj *obj;
645 unsigned long flags;
646
647 if (!debug_objects_enabled)
648 return;
649
650 db = get_bucket((unsigned long) addr);
651
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100652 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700653
654 obj = lookup_object(addr, db);
655 if (obj) {
656 switch (obj->state) {
657 case ODEBUG_STATE_INIT:
658 case ODEBUG_STATE_INACTIVE:
659 case ODEBUG_STATE_ACTIVE:
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400660 if (!obj->astate)
661 obj->state = ODEBUG_STATE_INACTIVE;
662 else
663 debug_print_object(obj, "deactivate");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700664 break;
665
666 case ODEBUG_STATE_DESTROYED:
667 debug_print_object(obj, "deactivate");
668 break;
669 default:
670 break;
671 }
672 } else {
673 struct debug_obj o = { .object = addr,
674 .state = ODEBUG_STATE_NOTAVAILABLE,
675 .descr = descr };
676
677 debug_print_object(&o, "deactivate");
678 }
679
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100680 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700681}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800682EXPORT_SYMBOL_GPL(debug_object_deactivate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700683
684/**
685 * debug_object_destroy - debug checks when an object is destroyed
686 * @addr: address of the object
687 * @descr: pointer to an object specific debug description structure
688 */
689void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
690{
691 enum debug_obj_state state;
692 struct debug_bucket *db;
693 struct debug_obj *obj;
694 unsigned long flags;
695
696 if (!debug_objects_enabled)
697 return;
698
699 db = get_bucket((unsigned long) addr);
700
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100701 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700702
703 obj = lookup_object(addr, db);
704 if (!obj)
705 goto out_unlock;
706
707 switch (obj->state) {
708 case ODEBUG_STATE_NONE:
709 case ODEBUG_STATE_INIT:
710 case ODEBUG_STATE_INACTIVE:
711 obj->state = ODEBUG_STATE_DESTROYED;
712 break;
713 case ODEBUG_STATE_ACTIVE:
714 debug_print_object(obj, "destroy");
715 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100716 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700717 debug_object_fixup(descr->fixup_destroy, addr, state);
718 return;
719
720 case ODEBUG_STATE_DESTROYED:
721 debug_print_object(obj, "destroy");
722 break;
723 default:
724 break;
725 }
726out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100727 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700728}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800729EXPORT_SYMBOL_GPL(debug_object_destroy);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700730
731/**
732 * debug_object_free - debug checks when an object is freed
733 * @addr: address of the object
734 * @descr: pointer to an object specific debug description structure
735 */
736void debug_object_free(void *addr, struct debug_obj_descr *descr)
737{
738 enum debug_obj_state state;
739 struct debug_bucket *db;
740 struct debug_obj *obj;
741 unsigned long flags;
742
743 if (!debug_objects_enabled)
744 return;
745
746 db = get_bucket((unsigned long) addr);
747
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100748 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700749
750 obj = lookup_object(addr, db);
751 if (!obj)
752 goto out_unlock;
753
754 switch (obj->state) {
755 case ODEBUG_STATE_ACTIVE:
756 debug_print_object(obj, "free");
757 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100758 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700759 debug_object_fixup(descr->fixup_free, addr, state);
760 return;
761 default:
762 hlist_del(&obj->node);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100763 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700764 free_object(obj);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200765 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700766 }
767out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100768 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700769}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800770EXPORT_SYMBOL_GPL(debug_object_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700771
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400772/**
Christine Chanb84d4352011-11-07 19:48:27 -0800773 * debug_object_assert_init - debug checks when object should be init-ed
774 * @addr: address of the object
775 * @descr: pointer to an object specific debug description structure
776 */
777void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
778{
779 struct debug_bucket *db;
780 struct debug_obj *obj;
781 unsigned long flags;
782
783 if (!debug_objects_enabled)
784 return;
785
786 db = get_bucket((unsigned long) addr);
787
788 raw_spin_lock_irqsave(&db->lock, flags);
789
790 obj = lookup_object(addr, db);
791 if (!obj) {
792 struct debug_obj o = { .object = addr,
793 .state = ODEBUG_STATE_NOTAVAILABLE,
794 .descr = descr };
795
796 raw_spin_unlock_irqrestore(&db->lock, flags);
797 /*
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700798 * Maybe the object is static, and we let the type specific
799 * code confirm. Track this static object if true, else invoke
800 * fixup.
Christine Chanb84d4352011-11-07 19:48:27 -0800801 */
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700802 if (descr->is_static_object && descr->is_static_object(addr)) {
803 /* Track this static object */
804 debug_object_init(addr, descr);
805 } else {
Christine Chanb84d4352011-11-07 19:48:27 -0800806 debug_print_object(&o, "assert_init");
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700807 debug_object_fixup(descr->fixup_assert_init, addr,
808 ODEBUG_STATE_NOTAVAILABLE);
809 }
Christine Chanb84d4352011-11-07 19:48:27 -0800810 return;
811 }
812
813 raw_spin_unlock_irqrestore(&db->lock, flags);
814}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800815EXPORT_SYMBOL_GPL(debug_object_assert_init);
Christine Chanb84d4352011-11-07 19:48:27 -0800816
817/**
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400818 * debug_object_active_state - debug checks object usage state machine
819 * @addr: address of the object
820 * @descr: pointer to an object specific debug description structure
821 * @expect: expected state
822 * @next: state to move to if expected state is found
823 */
824void
825debug_object_active_state(void *addr, struct debug_obj_descr *descr,
826 unsigned int expect, unsigned int next)
827{
828 struct debug_bucket *db;
829 struct debug_obj *obj;
830 unsigned long flags;
831
832 if (!debug_objects_enabled)
833 return;
834
835 db = get_bucket((unsigned long) addr);
836
837 raw_spin_lock_irqsave(&db->lock, flags);
838
839 obj = lookup_object(addr, db);
840 if (obj) {
841 switch (obj->state) {
842 case ODEBUG_STATE_ACTIVE:
843 if (obj->astate == expect)
844 obj->astate = next;
845 else
846 debug_print_object(obj, "active_state");
847 break;
848
849 default:
850 debug_print_object(obj, "active_state");
851 break;
852 }
853 } else {
854 struct debug_obj o = { .object = addr,
855 .state = ODEBUG_STATE_NOTAVAILABLE,
856 .descr = descr };
857
858 debug_print_object(&o, "active_state");
859 }
860
861 raw_spin_unlock_irqrestore(&db->lock, flags);
862}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800863EXPORT_SYMBOL_GPL(debug_object_active_state);
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400864
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700865#ifdef CONFIG_DEBUG_OBJECTS_FREE
866static void __debug_check_no_obj_freed(const void *address, unsigned long size)
867{
868 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700869 struct debug_obj_descr *descr;
870 enum debug_obj_state state;
871 struct debug_bucket *db;
Yang Shi1ea9b982018-02-06 07:18:28 +0800872 struct hlist_node *tmp;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700873 struct debug_obj *obj;
Yang Shibd9dcd02018-02-06 07:18:25 +0800874 int cnt, objs_checked = 0;
Yang Shi1ea9b982018-02-06 07:18:28 +0800875 bool work = false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700876
877 saddr = (unsigned long) address;
878 eaddr = saddr + size;
879 paddr = saddr & ODEBUG_CHUNK_MASK;
880 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
881 chunks >>= ODEBUG_CHUNK_SHIFT;
882
883 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
884 db = get_bucket(paddr);
885
886repeat:
887 cnt = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100888 raw_spin_lock_irqsave(&db->lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800889 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700890 cnt++;
891 oaddr = (unsigned long) obj->object;
892 if (oaddr < saddr || oaddr >= eaddr)
893 continue;
894
895 switch (obj->state) {
896 case ODEBUG_STATE_ACTIVE:
897 debug_print_object(obj, "free");
898 descr = obj->descr;
899 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100900 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700901 debug_object_fixup(descr->fixup_free,
902 (void *) oaddr, state);
903 goto repeat;
904 default:
905 hlist_del(&obj->node);
Yang Shi1ea9b982018-02-06 07:18:28 +0800906 work |= __free_object(obj);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700907 break;
908 }
909 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100910 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200911
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700912 if (cnt > debug_objects_maxchain)
913 debug_objects_maxchain = cnt;
Yang Shibd9dcd02018-02-06 07:18:25 +0800914
915 objs_checked += cnt;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700916 }
Yang Shibd9dcd02018-02-06 07:18:25 +0800917
918 if (objs_checked > debug_objects_maxchecked)
919 debug_objects_maxchecked = objs_checked;
Yang Shi1ea9b982018-02-06 07:18:28 +0800920
921 /* Schedule work to actually kmem_cache_free() objects */
922 if (work)
923 schedule_work(&debug_obj_work);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700924}
925
926void debug_check_no_obj_freed(const void *address, unsigned long size)
927{
928 if (debug_objects_enabled)
929 __debug_check_no_obj_freed(address, size);
930}
931#endif
932
933#ifdef CONFIG_DEBUG_FS
934
935static int debug_stats_show(struct seq_file *m, void *v)
936{
Waiman Longd86998b2019-05-20 10:14:46 -0400937 int cpu, obj_percpu_free = 0;
938
939 for_each_possible_cpu(cpu)
940 obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu);
941
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700942 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
Yang Shibd9dcd02018-02-06 07:18:25 +0800943 seq_printf(m, "max_checked :%d\n", debug_objects_maxchecked);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700944 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
945 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
Waiman Longd86998b2019-05-20 10:14:46 -0400946 seq_printf(m, "pool_free :%d\n", obj_pool_free + obj_percpu_free);
947 seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700948 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
Waiman Longd86998b2019-05-20 10:14:46 -0400949 seq_printf(m, "pool_used :%d\n", obj_pool_used - obj_percpu_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700950 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
Yang Shi36c4ead2018-02-06 07:18:26 +0800951 seq_printf(m, "on_free_list :%d\n", obj_nr_tofree);
Waiman Long0cad93c2017-02-07 16:40:30 -0500952 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
953 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700954 return 0;
955}
956
957static int debug_stats_open(struct inode *inode, struct file *filp)
958{
959 return single_open(filp, debug_stats_show, NULL);
960}
961
962static const struct file_operations debug_stats_fops = {
963 .open = debug_stats_open,
964 .read = seq_read,
965 .llseek = seq_lseek,
966 .release = single_release,
967};
968
969static int __init debug_objects_init_debugfs(void)
970{
Greg Kroah-Hartmanfecb0d92019-06-12 17:35:13 +0200971 struct dentry *dbgdir;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700972
973 if (!debug_objects_enabled)
974 return 0;
975
976 dbgdir = debugfs_create_dir("debug_objects", NULL);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700977
Greg Kroah-Hartmanfecb0d92019-06-12 17:35:13 +0200978 debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700979
980 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700981}
982__initcall(debug_objects_init_debugfs);
983
984#else
985static inline void debug_objects_init_debugfs(void) { }
986#endif
987
988#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
989
990/* Random data structure for the self test */
991struct self_test {
992 unsigned long dummy1[6];
993 int static_init;
994 unsigned long dummy2[3];
995};
996
997static __initdata struct debug_obj_descr descr_type_test;
998
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700999static bool __init is_static_object(void *addr)
1000{
1001 struct self_test *obj = addr;
1002
1003 return obj->static_init;
1004}
1005
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001006/*
1007 * fixup_init is called when:
1008 * - an active object is initialized
1009 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001010static bool __init fixup_init(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001011{
1012 struct self_test *obj = addr;
1013
1014 switch (state) {
1015 case ODEBUG_STATE_ACTIVE:
1016 debug_object_deactivate(obj, &descr_type_test);
1017 debug_object_init(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001018 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001019 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001020 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001021 }
1022}
1023
1024/*
1025 * fixup_activate is called when:
1026 * - an active object is activated
Du, Changbinb9fdac7f2016-05-19 17:09:41 -07001027 * - an unknown non-static object is activated
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001028 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001029static bool __init fixup_activate(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001030{
1031 struct self_test *obj = addr;
1032
1033 switch (state) {
1034 case ODEBUG_STATE_NOTAVAILABLE:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001035 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001036 case ODEBUG_STATE_ACTIVE:
1037 debug_object_deactivate(obj, &descr_type_test);
1038 debug_object_activate(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001039 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001040
1041 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001042 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001043 }
1044}
1045
1046/*
1047 * fixup_destroy is called when:
1048 * - an active object is destroyed
1049 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001050static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001051{
1052 struct self_test *obj = addr;
1053
1054 switch (state) {
1055 case ODEBUG_STATE_ACTIVE:
1056 debug_object_deactivate(obj, &descr_type_test);
1057 debug_object_destroy(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001058 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001059 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001060 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001061 }
1062}
1063
1064/*
1065 * fixup_free is called when:
1066 * - an active object is freed
1067 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001068static bool __init fixup_free(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001069{
1070 struct self_test *obj = addr;
1071
1072 switch (state) {
1073 case ODEBUG_STATE_ACTIVE:
1074 debug_object_deactivate(obj, &descr_type_test);
1075 debug_object_free(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001076 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001077 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001078 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001079 }
1080}
1081
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001082static int __init
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001083check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
1084{
1085 struct debug_bucket *db;
1086 struct debug_obj *obj;
1087 unsigned long flags;
1088 int res = -EINVAL;
1089
1090 db = get_bucket((unsigned long) addr);
1091
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001092 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001093
1094 obj = lookup_object(addr, db);
1095 if (!obj && state != ODEBUG_STATE_NONE) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001096 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001097 goto out;
1098 }
1099 if (obj && obj->state != state) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001100 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001101 obj->state, state);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001102 goto out;
1103 }
1104 if (fixups != debug_objects_fixups) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001105 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001106 fixups, debug_objects_fixups);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001107 goto out;
1108 }
1109 if (warnings != debug_objects_warnings) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001110 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001111 warnings, debug_objects_warnings);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001112 goto out;
1113 }
1114 res = 0;
1115out:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001116 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001117 if (res)
1118 debug_objects_enabled = 0;
1119 return res;
1120}
1121
1122static __initdata struct debug_obj_descr descr_type_test = {
1123 .name = "selftest",
Du, Changbinb9fdac7f2016-05-19 17:09:41 -07001124 .is_static_object = is_static_object,
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001125 .fixup_init = fixup_init,
1126 .fixup_activate = fixup_activate,
1127 .fixup_destroy = fixup_destroy,
1128 .fixup_free = fixup_free,
1129};
1130
1131static __initdata struct self_test obj = { .static_init = 0 };
1132
1133static void __init debug_objects_selftest(void)
1134{
1135 int fixups, oldfixups, warnings, oldwarnings;
1136 unsigned long flags;
1137
1138 local_irq_save(flags);
1139
1140 fixups = oldfixups = debug_objects_fixups;
1141 warnings = oldwarnings = debug_objects_warnings;
1142 descr_test = &descr_type_test;
1143
1144 debug_object_init(&obj, &descr_type_test);
1145 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1146 goto out;
1147 debug_object_activate(&obj, &descr_type_test);
1148 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1149 goto out;
1150 debug_object_activate(&obj, &descr_type_test);
1151 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1152 goto out;
1153 debug_object_deactivate(&obj, &descr_type_test);
1154 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1155 goto out;
1156 debug_object_destroy(&obj, &descr_type_test);
1157 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1158 goto out;
1159 debug_object_init(&obj, &descr_type_test);
1160 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1161 goto out;
1162 debug_object_activate(&obj, &descr_type_test);
1163 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1164 goto out;
1165 debug_object_deactivate(&obj, &descr_type_test);
1166 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1167 goto out;
1168 debug_object_free(&obj, &descr_type_test);
1169 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1170 goto out;
1171
1172 obj.static_init = 1;
1173 debug_object_activate(&obj, &descr_type_test);
Stephen Boyd9f78ff02012-03-05 14:59:17 -08001174 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001175 goto out;
1176 debug_object_init(&obj, &descr_type_test);
1177 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1178 goto out;
1179 debug_object_free(&obj, &descr_type_test);
1180 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1181 goto out;
1182
1183#ifdef CONFIG_DEBUG_OBJECTS_FREE
1184 debug_object_init(&obj, &descr_type_test);
1185 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1186 goto out;
1187 debug_object_activate(&obj, &descr_type_test);
1188 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1189 goto out;
1190 __debug_check_no_obj_freed(&obj, sizeof(obj));
1191 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1192 goto out;
1193#endif
Fabian Frederick719e4842014-06-04 16:06:04 -07001194 pr_info("selftest passed\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001195
1196out:
1197 debug_objects_fixups = oldfixups;
1198 debug_objects_warnings = oldwarnings;
1199 descr_test = NULL;
1200
1201 local_irq_restore(flags);
1202}
1203#else
1204static inline void debug_objects_selftest(void) { }
1205#endif
1206
1207/*
1208 * Called during early boot to initialize the hash buckets and link
1209 * the static object pool objects into the poll list. After this call
1210 * the object tracker is fully operational.
1211 */
1212void __init debug_objects_early_init(void)
1213{
1214 int i;
1215
1216 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001217 raw_spin_lock_init(&obj_hash[i].lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001218
1219 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1220 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1221}
1222
1223/*
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001224 * Convert the statically allocated objects to dynamic ones:
1225 */
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001226static int __init debug_objects_replace_static_objects(void)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001227{
1228 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001229 struct hlist_node *tmp;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001230 struct debug_obj *obj, *new;
1231 HLIST_HEAD(objects);
1232 int i, cnt = 0;
1233
1234 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1235 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1236 if (!obj)
1237 goto free;
1238 hlist_add_head(&obj->node, &objects);
1239 }
1240
1241 /*
Qian Caia9ee3a62018-12-28 00:32:32 -08001242 * debug_objects_mem_init() is now called early that only one CPU is up
1243 * and interrupts have been disabled, so it is safe to replace the
1244 * active object references.
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001245 */
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001246
1247 /* Remove the statically allocated objects from the pool */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001248 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001249 hlist_del(&obj->node);
1250 /* Move the allocated objects to the pool */
1251 hlist_move_list(&objects, &obj_pool);
1252
1253 /* Replace the active object references */
1254 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1255 hlist_move_list(&db->list, &objects);
1256
Sasha Levinb67bfe02013-02-27 17:06:00 -08001257 hlist_for_each_entry(obj, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001258 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1259 hlist_del(&new->node);
1260 /* copy object data */
1261 *new = *obj;
1262 hlist_add_head(&new->node, &db->list);
1263 cnt++;
1264 }
1265 }
1266
Fabian Frederickc0f35cc2014-06-04 16:06:05 -07001267 pr_debug("%d of %d active objects replaced\n",
1268 cnt, obj_pool_used);
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001269 return 0;
1270free:
Sasha Levinb67bfe02013-02-27 17:06:00 -08001271 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001272 hlist_del(&obj->node);
1273 kmem_cache_free(obj_cache, obj);
1274 }
1275 return -ENOMEM;
1276}
1277
1278/*
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001279 * Called after the kmem_caches are functional to setup a dedicated
1280 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1281 * prevents that the debug code is called on kmem_cache_free() for the
1282 * debug tracker objects to avoid recursive calls.
1283 */
1284void __init debug_objects_mem_init(void)
1285{
Waiman Long634d61f2019-05-20 10:14:47 -04001286 int cpu, extras;
Waiman Longd86998b2019-05-20 10:14:46 -04001287
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001288 if (!debug_objects_enabled)
1289 return;
1290
Waiman Longd86998b2019-05-20 10:14:46 -04001291 /*
1292 * Initialize the percpu object pools
1293 *
1294 * Initialization is not strictly necessary, but was done for
1295 * completeness.
1296 */
1297 for_each_possible_cpu(cpu)
1298 INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool.free_objs, cpu));
1299
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001300 obj_cache = kmem_cache_create("debug_objects_cache",
1301 sizeof (struct debug_obj), 0,
Qian Cai8de456c2018-11-30 14:09:48 -08001302 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
1303 NULL);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001304
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001305 if (!obj_cache || debug_objects_replace_static_objects()) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001306 debug_objects_enabled = 0;
Zhong Jiang3ff4f802018-08-01 00:24:58 +08001307 kmem_cache_destroy(obj_cache);
Fabian Frederick719e4842014-06-04 16:06:04 -07001308 pr_warn("out of memory.\n");
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001309 } else
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001310 debug_objects_selftest();
Waiman Long634d61f2019-05-20 10:14:47 -04001311
1312 /*
1313 * Increase the thresholds for allocating and freeing objects
1314 * according to the number of possible CPUs available in the system.
1315 */
1316 extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
1317 debug_objects_pool_size += extras;
1318 debug_objects_pool_min_level += extras;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001319}