blob: 7ea19fa6356160462260313a2d531f66b55ac22e [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;
Waiman Longd26bf502019-05-20 10:14:48 -0400123 struct debug_obj *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 */
Waiman Longd26bf502019-05-20 10:14:48 -0400139 while (obj_nr_tofree && (obj_pool_free < obj_pool_min_free)) {
Yang Shi36c4ead2018-02-06 07:18:26 +0800140 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) {
Waiman Longd26bf502019-05-20 10:14:48 -0400153 struct debug_obj *new[ODEBUG_BATCH_SIZE];
154 int cnt;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700155
Waiman Longd26bf502019-05-20 10:14:48 -0400156 for (cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) {
157 new[cnt] = kmem_cache_zalloc(obj_cache, gfp);
158 if (!new[cnt])
159 break;
160 }
161 if (!cnt)
Dan Carpenter33408082012-04-18 14:28:10 +0300162 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700163
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100164 raw_spin_lock_irqsave(&pool_lock, flags);
Waiman Longd26bf502019-05-20 10:14:48 -0400165 while (cnt) {
166 hlist_add_head(&new[--cnt]->node, &obj_pool);
167 debug_objects_allocated++;
168 obj_pool_free++;
169 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100170 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700171 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700172}
173
174/*
175 * Lookup an object in the hash bucket.
176 */
177static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
178{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700179 struct debug_obj *obj;
180 int cnt = 0;
181
Sasha Levinb67bfe02013-02-27 17:06:00 -0800182 hlist_for_each_entry(obj, &b->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700183 cnt++;
184 if (obj->object == addr)
185 return obj;
186 }
187 if (cnt > debug_objects_maxchain)
188 debug_objects_maxchain = cnt;
189
190 return NULL;
191}
192
193/*
Waiman Longd86998b2019-05-20 10:14:46 -0400194 * Allocate a new object from the hlist
195 */
196static struct debug_obj *__alloc_object(struct hlist_head *list)
197{
198 struct debug_obj *obj = NULL;
199
200 if (list->first) {
201 obj = hlist_entry(list->first, typeof(*obj), node);
202 hlist_del(&obj->node);
203 }
204
205 return obj;
206}
207
208/*
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200209 * Allocate a new object. If the pool is empty, switch off the debugger.
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200210 * Must be called with interrupts disabled.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700211 */
212static struct debug_obj *
213alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
214{
Waiman Long634d61f2019-05-20 10:14:47 -0400215 struct debug_percpu_free *percpu_pool = this_cpu_ptr(&percpu_obj_pool);
Waiman Longd86998b2019-05-20 10:14:46 -0400216 struct debug_obj *obj;
217
218 if (likely(obj_cache)) {
Waiman Longd86998b2019-05-20 10:14:46 -0400219 obj = __alloc_object(&percpu_pool->free_objs);
220 if (obj) {
221 percpu_pool->obj_free--;
222 goto init_obj;
223 }
224 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700225
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100226 raw_spin_lock(&pool_lock);
Waiman Longd86998b2019-05-20 10:14:46 -0400227 obj = __alloc_object(&obj_pool);
228 if (obj) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700229 obj_pool_used++;
Waiman Long634d61f2019-05-20 10:14:47 -0400230 obj_pool_free--;
231
232 /*
233 * Looking ahead, allocate one batch of debug objects and
234 * put them into the percpu free pool.
235 */
236 if (likely(obj_cache)) {
237 int i;
238
239 for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
240 struct debug_obj *obj2;
241
242 obj2 = __alloc_object(&obj_pool);
243 if (!obj2)
244 break;
245 hlist_add_head(&obj2->node,
246 &percpu_pool->free_objs);
247 percpu_pool->obj_free++;
248 obj_pool_used++;
249 obj_pool_free--;
250 }
251 }
252
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700253 if (obj_pool_used > obj_pool_max_used)
254 obj_pool_max_used = obj_pool_used;
255
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700256 if (obj_pool_free < obj_pool_min_free)
257 obj_pool_min_free = obj_pool_free;
258 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100259 raw_spin_unlock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700260
Waiman Longd86998b2019-05-20 10:14:46 -0400261init_obj:
262 if (obj) {
263 obj->object = addr;
264 obj->descr = descr;
265 obj->state = ODEBUG_STATE_NONE;
266 obj->astate = 0;
267 hlist_add_head(&obj->node, &b->list);
268 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700269 return obj;
270}
271
272/*
Thomas Gleixner337fff82009-03-16 10:04:53 +0100273 * workqueue function to free objects.
Waiman Long858274b2017-01-05 15:17:05 -0500274 *
275 * To reduce contention on the global pool_lock, the actual freeing of
Yang Shi636e1972018-02-06 07:18:27 +0800276 * debug objects will be delayed if the pool_lock is busy.
Thomas Gleixner337fff82009-03-16 10:04:53 +0100277 */
278static void free_obj_work(struct work_struct *work)
279{
Yang Shi36c4ead2018-02-06 07:18:26 +0800280 struct hlist_node *tmp;
281 struct debug_obj *obj;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100282 unsigned long flags;
Yang Shi36c4ead2018-02-06 07:18:26 +0800283 HLIST_HEAD(tofree);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100284
Waiman Long858274b2017-01-05 15:17:05 -0500285 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
286 return;
Yang Shi36c4ead2018-02-06 07:18:26 +0800287
288 /*
289 * The objs on the pool list might be allocated before the work is
290 * run, so recheck if pool list it full or not, if not fill pool
Waiman Longd26bf502019-05-20 10:14:48 -0400291 * list from the global free list.
Yang Shi36c4ead2018-02-06 07:18:26 +0800292 */
293 while (obj_nr_tofree && obj_pool_free < debug_objects_pool_size) {
294 obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
295 hlist_del(&obj->node);
296 hlist_add_head(&obj->node, &obj_pool);
297 obj_pool_free++;
298 obj_nr_tofree--;
299 }
300
301 /*
302 * Pool list is already full and there are still objs on the free
303 * list. Move remaining free objs to a temporary list to free the
304 * memory outside the pool_lock held region.
305 */
306 if (obj_nr_tofree) {
307 hlist_move_list(&obj_to_free, &tofree);
Arnd Bergmann04148182018-02-22 16:52:58 +0100308 debug_objects_freed += obj_nr_tofree;
Yang Shi36c4ead2018-02-06 07:18:26 +0800309 obj_nr_tofree = 0;
310 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100311 raw_spin_unlock_irqrestore(&pool_lock, flags);
Yang Shi36c4ead2018-02-06 07:18:26 +0800312
313 hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
314 hlist_del(&obj->node);
315 kmem_cache_free(obj_cache, obj);
316 }
Thomas Gleixner337fff82009-03-16 10:04:53 +0100317}
318
Yang Shi636e1972018-02-06 07:18:27 +0800319static bool __free_object(struct debug_obj *obj)
320{
Waiman Long634d61f2019-05-20 10:14:47 -0400321 struct debug_obj *objs[ODEBUG_BATCH_SIZE];
322 struct debug_percpu_free *percpu_pool;
323 int lookahead_count = 0;
Yang Shi636e1972018-02-06 07:18:27 +0800324 unsigned long flags;
325 bool work;
326
Waiman Longd86998b2019-05-20 10:14:46 -0400327 local_irq_save(flags);
Waiman Long634d61f2019-05-20 10:14:47 -0400328 if (!obj_cache)
329 goto free_to_obj_pool;
330
Waiman Longd86998b2019-05-20 10:14:46 -0400331 /*
332 * Try to free it into the percpu pool first.
333 */
334 percpu_pool = this_cpu_ptr(&percpu_obj_pool);
Waiman Long634d61f2019-05-20 10:14:47 -0400335 if (percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) {
Waiman Longd86998b2019-05-20 10:14:46 -0400336 hlist_add_head(&obj->node, &percpu_pool->free_objs);
337 percpu_pool->obj_free++;
338 local_irq_restore(flags);
339 return false;
340 }
341
Waiman Long634d61f2019-05-20 10:14:47 -0400342 /*
343 * As the percpu pool is full, look ahead and pull out a batch
344 * of objects from the percpu pool and free them as well.
345 */
346 for (; lookahead_count < ODEBUG_BATCH_SIZE; lookahead_count++) {
347 objs[lookahead_count] = __alloc_object(&percpu_pool->free_objs);
348 if (!objs[lookahead_count])
349 break;
350 percpu_pool->obj_free--;
351 }
352
353free_to_obj_pool:
Waiman Longd86998b2019-05-20 10:14:46 -0400354 raw_spin_lock(&pool_lock);
Yang Shi636e1972018-02-06 07:18:27 +0800355 work = (obj_pool_free > debug_objects_pool_size) && obj_cache;
356 obj_pool_used--;
357
358 if (work) {
359 obj_nr_tofree++;
360 hlist_add_head(&obj->node, &obj_to_free);
Waiman Long634d61f2019-05-20 10:14:47 -0400361 if (lookahead_count) {
362 obj_nr_tofree += lookahead_count;
363 obj_pool_used -= lookahead_count;
364 while (lookahead_count) {
365 hlist_add_head(&objs[--lookahead_count]->node,
366 &obj_to_free);
367 }
368 }
Yang Shi636e1972018-02-06 07:18:27 +0800369 } else {
370 obj_pool_free++;
371 hlist_add_head(&obj->node, &obj_pool);
Waiman Long634d61f2019-05-20 10:14:47 -0400372 if (lookahead_count) {
373 obj_pool_free += lookahead_count;
374 obj_pool_used -= lookahead_count;
375 while (lookahead_count) {
376 hlist_add_head(&objs[--lookahead_count]->node,
377 &obj_pool);
378 }
379 }
Yang Shi636e1972018-02-06 07:18:27 +0800380 }
Waiman Longd86998b2019-05-20 10:14:46 -0400381 raw_spin_unlock(&pool_lock);
382 local_irq_restore(flags);
Yang Shi636e1972018-02-06 07:18:27 +0800383 return work;
384}
385
Thomas Gleixner337fff82009-03-16 10:04:53 +0100386/*
387 * Put the object back into the pool and schedule work to free objects
388 * if necessary.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700389 */
390static void free_object(struct debug_obj *obj)
391{
Yang Shi636e1972018-02-06 07:18:27 +0800392 if (__free_object(obj))
Thomas Gleixner337fff82009-03-16 10:04:53 +0100393 schedule_work(&debug_obj_work);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700394}
395
396/*
397 * We run out of memory. That means we probably have tons of objects
398 * allocated.
399 */
400static void debug_objects_oom(void)
401{
402 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800403 struct hlist_node *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200404 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700405 struct debug_obj *obj;
406 unsigned long flags;
407 int i;
408
Fabian Frederick719e4842014-06-04 16:06:04 -0700409 pr_warn("Out of memory. ODEBUG disabled\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700410
411 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100412 raw_spin_lock_irqsave(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200413 hlist_move_list(&db->list, &freelist);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100414 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200415
416 /* Now free them */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800417 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700418 hlist_del(&obj->node);
419 free_object(obj);
420 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700421 }
422}
423
424/*
425 * We use the pfn of the address for the hash. That way we can check
426 * for freed objects simply by checking the affected bucket.
427 */
428static struct debug_bucket *get_bucket(unsigned long addr)
429{
430 unsigned long hash;
431
432 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
433 return &obj_hash[hash];
434}
435
436static void debug_print_object(struct debug_obj *obj, char *msg)
437{
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100438 struct debug_obj_descr *descr = obj->descr;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700439 static int limit;
440
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100441 if (limit < 5 && descr != descr_test) {
442 void *hint = descr->debug_hint ?
443 descr->debug_hint(obj->object) : NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700444 limit++;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400445 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100446 "object type: %s hint: %pS\n",
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400447 msg, obj_states[obj->state], obj->astate,
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100448 descr->name, hint);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700449 }
450 debug_objects_warnings++;
451}
452
453/*
454 * Try to repair the damage, so we have a better chance to get useful
455 * debug output.
456 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700457static bool
458debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700459 void * addr, enum debug_obj_state state)
460{
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700461 if (fixup && fixup(addr, state)) {
462 debug_objects_fixups++;
463 return true;
464 }
465 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700466}
467
468static void debug_object_is_on_stack(void *addr, int onstack)
469{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700470 int is_on_stack;
471 static int limit;
472
473 if (limit > 4)
474 return;
475
FUJITA Tomonori8b05c7e2008-07-23 21:26:53 -0700476 is_on_stack = object_is_on_stack(addr);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700477 if (is_on_stack == onstack)
478 return;
479
480 limit++;
481 if (is_on_stack)
Joel Fernandes (Google)fc91a3c2018-07-23 14:25:31 -0700482 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
483 task_stack_page(current));
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700484 else
Joel Fernandes (Google)fc91a3c2018-07-23 14:25:31 -0700485 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
486 task_stack_page(current));
487
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700488 WARN_ON(1);
489}
490
491static void
492__debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
493{
494 enum debug_obj_state state;
495 struct debug_bucket *db;
496 struct debug_obj *obj;
497 unsigned long flags;
498
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200499 fill_pool();
500
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700501 db = get_bucket((unsigned long) addr);
502
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100503 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700504
505 obj = lookup_object(addr, db);
506 if (!obj) {
507 obj = alloc_object(addr, db, descr);
508 if (!obj) {
509 debug_objects_enabled = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100510 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700511 debug_objects_oom();
512 return;
513 }
514 debug_object_is_on_stack(addr, onstack);
515 }
516
517 switch (obj->state) {
518 case ODEBUG_STATE_NONE:
519 case ODEBUG_STATE_INIT:
520 case ODEBUG_STATE_INACTIVE:
521 obj->state = ODEBUG_STATE_INIT;
522 break;
523
524 case ODEBUG_STATE_ACTIVE:
525 debug_print_object(obj, "init");
526 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100527 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700528 debug_object_fixup(descr->fixup_init, addr, state);
529 return;
530
531 case ODEBUG_STATE_DESTROYED:
532 debug_print_object(obj, "init");
533 break;
534 default:
535 break;
536 }
537
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100538 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700539}
540
541/**
542 * debug_object_init - debug checks when an object is initialized
543 * @addr: address of the object
544 * @descr: pointer to an object specific debug description structure
545 */
546void debug_object_init(void *addr, struct debug_obj_descr *descr)
547{
548 if (!debug_objects_enabled)
549 return;
550
551 __debug_object_init(addr, descr, 0);
552}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800553EXPORT_SYMBOL_GPL(debug_object_init);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700554
555/**
556 * debug_object_init_on_stack - debug checks when an object on stack is
557 * initialized
558 * @addr: address of the object
559 * @descr: pointer to an object specific debug description structure
560 */
561void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
562{
563 if (!debug_objects_enabled)
564 return;
565
566 __debug_object_init(addr, descr, 1);
567}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800568EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700569
570/**
571 * debug_object_activate - debug checks when an object is activated
572 * @addr: address of the object
573 * @descr: pointer to an object specific debug description structure
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700574 * Returns 0 for success, -EINVAL for check failed.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700575 */
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700576int debug_object_activate(void *addr, struct debug_obj_descr *descr)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700577{
578 enum debug_obj_state state;
579 struct debug_bucket *db;
580 struct debug_obj *obj;
581 unsigned long flags;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700582 int ret;
Stephen Boydfeac18d2011-11-07 19:48:26 -0800583 struct debug_obj o = { .object = addr,
584 .state = ODEBUG_STATE_NOTAVAILABLE,
585 .descr = descr };
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700586
587 if (!debug_objects_enabled)
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700588 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700589
590 db = get_bucket((unsigned long) addr);
591
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100592 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700593
594 obj = lookup_object(addr, db);
595 if (obj) {
596 switch (obj->state) {
597 case ODEBUG_STATE_INIT:
598 case ODEBUG_STATE_INACTIVE:
599 obj->state = ODEBUG_STATE_ACTIVE;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700600 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700601 break;
602
603 case ODEBUG_STATE_ACTIVE:
604 debug_print_object(obj, "activate");
605 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100606 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700607 ret = debug_object_fixup(descr->fixup_activate, addr, state);
Du, Changbine7a8e782016-05-19 17:09:23 -0700608 return ret ? 0 : -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700609
610 case ODEBUG_STATE_DESTROYED:
611 debug_print_object(obj, "activate");
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700612 ret = -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700613 break;
614 default:
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700615 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700616 break;
617 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100618 raw_spin_unlock_irqrestore(&db->lock, flags);
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700619 return ret;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700620 }
621
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100622 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700623 /*
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700624 * We are here when a static object is activated. We
625 * let the type specific code confirm whether this is
626 * true or not. if true, we just make sure that the
627 * static object is tracked in the object tracker. If
628 * not, this must be a bug, so we try to fix it up.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700629 */
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700630 if (descr->is_static_object && descr->is_static_object(addr)) {
631 /* track this static object */
632 debug_object_init(addr, descr);
633 debug_object_activate(addr, descr);
634 } else {
Stephen Boydfeac18d2011-11-07 19:48:26 -0800635 debug_print_object(&o, "activate");
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700636 ret = debug_object_fixup(descr->fixup_activate, addr,
637 ODEBUG_STATE_NOTAVAILABLE);
638 return ret ? 0 : -EINVAL;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700639 }
640 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700641}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800642EXPORT_SYMBOL_GPL(debug_object_activate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700643
644/**
645 * debug_object_deactivate - debug checks when an object is deactivated
646 * @addr: address of the object
647 * @descr: pointer to an object specific debug description structure
648 */
649void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
650{
651 struct debug_bucket *db;
652 struct debug_obj *obj;
653 unsigned long flags;
654
655 if (!debug_objects_enabled)
656 return;
657
658 db = get_bucket((unsigned long) addr);
659
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100660 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700661
662 obj = lookup_object(addr, db);
663 if (obj) {
664 switch (obj->state) {
665 case ODEBUG_STATE_INIT:
666 case ODEBUG_STATE_INACTIVE:
667 case ODEBUG_STATE_ACTIVE:
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400668 if (!obj->astate)
669 obj->state = ODEBUG_STATE_INACTIVE;
670 else
671 debug_print_object(obj, "deactivate");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700672 break;
673
674 case ODEBUG_STATE_DESTROYED:
675 debug_print_object(obj, "deactivate");
676 break;
677 default:
678 break;
679 }
680 } else {
681 struct debug_obj o = { .object = addr,
682 .state = ODEBUG_STATE_NOTAVAILABLE,
683 .descr = descr };
684
685 debug_print_object(&o, "deactivate");
686 }
687
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100688 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700689}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800690EXPORT_SYMBOL_GPL(debug_object_deactivate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700691
692/**
693 * debug_object_destroy - debug checks when an object is destroyed
694 * @addr: address of the object
695 * @descr: pointer to an object specific debug description structure
696 */
697void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
698{
699 enum debug_obj_state state;
700 struct debug_bucket *db;
701 struct debug_obj *obj;
702 unsigned long flags;
703
704 if (!debug_objects_enabled)
705 return;
706
707 db = get_bucket((unsigned long) addr);
708
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100709 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700710
711 obj = lookup_object(addr, db);
712 if (!obj)
713 goto out_unlock;
714
715 switch (obj->state) {
716 case ODEBUG_STATE_NONE:
717 case ODEBUG_STATE_INIT:
718 case ODEBUG_STATE_INACTIVE:
719 obj->state = ODEBUG_STATE_DESTROYED;
720 break;
721 case ODEBUG_STATE_ACTIVE:
722 debug_print_object(obj, "destroy");
723 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100724 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700725 debug_object_fixup(descr->fixup_destroy, addr, state);
726 return;
727
728 case ODEBUG_STATE_DESTROYED:
729 debug_print_object(obj, "destroy");
730 break;
731 default:
732 break;
733 }
734out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100735 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700736}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800737EXPORT_SYMBOL_GPL(debug_object_destroy);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700738
739/**
740 * debug_object_free - debug checks when an object is freed
741 * @addr: address of the object
742 * @descr: pointer to an object specific debug description structure
743 */
744void debug_object_free(void *addr, struct debug_obj_descr *descr)
745{
746 enum debug_obj_state state;
747 struct debug_bucket *db;
748 struct debug_obj *obj;
749 unsigned long flags;
750
751 if (!debug_objects_enabled)
752 return;
753
754 db = get_bucket((unsigned long) addr);
755
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100756 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700757
758 obj = lookup_object(addr, db);
759 if (!obj)
760 goto out_unlock;
761
762 switch (obj->state) {
763 case ODEBUG_STATE_ACTIVE:
764 debug_print_object(obj, "free");
765 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100766 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700767 debug_object_fixup(descr->fixup_free, addr, state);
768 return;
769 default:
770 hlist_del(&obj->node);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100771 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700772 free_object(obj);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200773 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700774 }
775out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100776 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700777}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800778EXPORT_SYMBOL_GPL(debug_object_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700779
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400780/**
Christine Chanb84d4352011-11-07 19:48:27 -0800781 * debug_object_assert_init - debug checks when object should be init-ed
782 * @addr: address of the object
783 * @descr: pointer to an object specific debug description structure
784 */
785void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
786{
787 struct debug_bucket *db;
788 struct debug_obj *obj;
789 unsigned long flags;
790
791 if (!debug_objects_enabled)
792 return;
793
794 db = get_bucket((unsigned long) addr);
795
796 raw_spin_lock_irqsave(&db->lock, flags);
797
798 obj = lookup_object(addr, db);
799 if (!obj) {
800 struct debug_obj o = { .object = addr,
801 .state = ODEBUG_STATE_NOTAVAILABLE,
802 .descr = descr };
803
804 raw_spin_unlock_irqrestore(&db->lock, flags);
805 /*
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700806 * Maybe the object is static, and we let the type specific
807 * code confirm. Track this static object if true, else invoke
808 * fixup.
Christine Chanb84d4352011-11-07 19:48:27 -0800809 */
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700810 if (descr->is_static_object && descr->is_static_object(addr)) {
811 /* Track this static object */
812 debug_object_init(addr, descr);
813 } else {
Christine Chanb84d4352011-11-07 19:48:27 -0800814 debug_print_object(&o, "assert_init");
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700815 debug_object_fixup(descr->fixup_assert_init, addr,
816 ODEBUG_STATE_NOTAVAILABLE);
817 }
Christine Chanb84d4352011-11-07 19:48:27 -0800818 return;
819 }
820
821 raw_spin_unlock_irqrestore(&db->lock, flags);
822}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800823EXPORT_SYMBOL_GPL(debug_object_assert_init);
Christine Chanb84d4352011-11-07 19:48:27 -0800824
825/**
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400826 * debug_object_active_state - debug checks object usage state machine
827 * @addr: address of the object
828 * @descr: pointer to an object specific debug description structure
829 * @expect: expected state
830 * @next: state to move to if expected state is found
831 */
832void
833debug_object_active_state(void *addr, struct debug_obj_descr *descr,
834 unsigned int expect, unsigned int next)
835{
836 struct debug_bucket *db;
837 struct debug_obj *obj;
838 unsigned long flags;
839
840 if (!debug_objects_enabled)
841 return;
842
843 db = get_bucket((unsigned long) addr);
844
845 raw_spin_lock_irqsave(&db->lock, flags);
846
847 obj = lookup_object(addr, db);
848 if (obj) {
849 switch (obj->state) {
850 case ODEBUG_STATE_ACTIVE:
851 if (obj->astate == expect)
852 obj->astate = next;
853 else
854 debug_print_object(obj, "active_state");
855 break;
856
857 default:
858 debug_print_object(obj, "active_state");
859 break;
860 }
861 } else {
862 struct debug_obj o = { .object = addr,
863 .state = ODEBUG_STATE_NOTAVAILABLE,
864 .descr = descr };
865
866 debug_print_object(&o, "active_state");
867 }
868
869 raw_spin_unlock_irqrestore(&db->lock, flags);
870}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800871EXPORT_SYMBOL_GPL(debug_object_active_state);
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400872
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700873#ifdef CONFIG_DEBUG_OBJECTS_FREE
874static void __debug_check_no_obj_freed(const void *address, unsigned long size)
875{
876 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700877 struct debug_obj_descr *descr;
878 enum debug_obj_state state;
879 struct debug_bucket *db;
Yang Shi1ea9b982018-02-06 07:18:28 +0800880 struct hlist_node *tmp;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700881 struct debug_obj *obj;
Yang Shibd9dcd02018-02-06 07:18:25 +0800882 int cnt, objs_checked = 0;
Yang Shi1ea9b982018-02-06 07:18:28 +0800883 bool work = false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700884
885 saddr = (unsigned long) address;
886 eaddr = saddr + size;
887 paddr = saddr & ODEBUG_CHUNK_MASK;
888 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
889 chunks >>= ODEBUG_CHUNK_SHIFT;
890
891 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
892 db = get_bucket(paddr);
893
894repeat:
895 cnt = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100896 raw_spin_lock_irqsave(&db->lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800897 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700898 cnt++;
899 oaddr = (unsigned long) obj->object;
900 if (oaddr < saddr || oaddr >= eaddr)
901 continue;
902
903 switch (obj->state) {
904 case ODEBUG_STATE_ACTIVE:
905 debug_print_object(obj, "free");
906 descr = obj->descr;
907 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100908 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700909 debug_object_fixup(descr->fixup_free,
910 (void *) oaddr, state);
911 goto repeat;
912 default:
913 hlist_del(&obj->node);
Yang Shi1ea9b982018-02-06 07:18:28 +0800914 work |= __free_object(obj);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700915 break;
916 }
917 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100918 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200919
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700920 if (cnt > debug_objects_maxchain)
921 debug_objects_maxchain = cnt;
Yang Shibd9dcd02018-02-06 07:18:25 +0800922
923 objs_checked += cnt;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700924 }
Yang Shibd9dcd02018-02-06 07:18:25 +0800925
926 if (objs_checked > debug_objects_maxchecked)
927 debug_objects_maxchecked = objs_checked;
Yang Shi1ea9b982018-02-06 07:18:28 +0800928
929 /* Schedule work to actually kmem_cache_free() objects */
930 if (work)
931 schedule_work(&debug_obj_work);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700932}
933
934void debug_check_no_obj_freed(const void *address, unsigned long size)
935{
936 if (debug_objects_enabled)
937 __debug_check_no_obj_freed(address, size);
938}
939#endif
940
941#ifdef CONFIG_DEBUG_FS
942
943static int debug_stats_show(struct seq_file *m, void *v)
944{
Waiman Longd86998b2019-05-20 10:14:46 -0400945 int cpu, obj_percpu_free = 0;
946
947 for_each_possible_cpu(cpu)
948 obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu);
949
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700950 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
Yang Shibd9dcd02018-02-06 07:18:25 +0800951 seq_printf(m, "max_checked :%d\n", debug_objects_maxchecked);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700952 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
953 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
Waiman Longd86998b2019-05-20 10:14:46 -0400954 seq_printf(m, "pool_free :%d\n", obj_pool_free + obj_percpu_free);
955 seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700956 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
Waiman Longd86998b2019-05-20 10:14:46 -0400957 seq_printf(m, "pool_used :%d\n", obj_pool_used - obj_percpu_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700958 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
Yang Shi36c4ead2018-02-06 07:18:26 +0800959 seq_printf(m, "on_free_list :%d\n", obj_nr_tofree);
Waiman Long0cad93c2017-02-07 16:40:30 -0500960 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
961 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700962 return 0;
963}
964
965static int debug_stats_open(struct inode *inode, struct file *filp)
966{
967 return single_open(filp, debug_stats_show, NULL);
968}
969
970static const struct file_operations debug_stats_fops = {
971 .open = debug_stats_open,
972 .read = seq_read,
973 .llseek = seq_lseek,
974 .release = single_release,
975};
976
977static int __init debug_objects_init_debugfs(void)
978{
Greg Kroah-Hartmanfecb0d92019-06-12 17:35:13 +0200979 struct dentry *dbgdir;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700980
981 if (!debug_objects_enabled)
982 return 0;
983
984 dbgdir = debugfs_create_dir("debug_objects", NULL);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700985
Greg Kroah-Hartmanfecb0d92019-06-12 17:35:13 +0200986 debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700987
988 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700989}
990__initcall(debug_objects_init_debugfs);
991
992#else
993static inline void debug_objects_init_debugfs(void) { }
994#endif
995
996#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
997
998/* Random data structure for the self test */
999struct self_test {
1000 unsigned long dummy1[6];
1001 int static_init;
1002 unsigned long dummy2[3];
1003};
1004
1005static __initdata struct debug_obj_descr descr_type_test;
1006
Du, Changbinb9fdac7f2016-05-19 17:09:41 -07001007static bool __init is_static_object(void *addr)
1008{
1009 struct self_test *obj = addr;
1010
1011 return obj->static_init;
1012}
1013
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001014/*
1015 * fixup_init is called when:
1016 * - an active object is initialized
1017 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001018static bool __init fixup_init(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001019{
1020 struct self_test *obj = addr;
1021
1022 switch (state) {
1023 case ODEBUG_STATE_ACTIVE:
1024 debug_object_deactivate(obj, &descr_type_test);
1025 debug_object_init(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001026 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001027 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001028 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001029 }
1030}
1031
1032/*
1033 * fixup_activate is called when:
1034 * - an active object is activated
Du, Changbinb9fdac7f2016-05-19 17:09:41 -07001035 * - an unknown non-static object is activated
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001036 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001037static bool __init fixup_activate(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001038{
1039 struct self_test *obj = addr;
1040
1041 switch (state) {
1042 case ODEBUG_STATE_NOTAVAILABLE:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001043 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001044 case ODEBUG_STATE_ACTIVE:
1045 debug_object_deactivate(obj, &descr_type_test);
1046 debug_object_activate(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001047 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001048
1049 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001050 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001051 }
1052}
1053
1054/*
1055 * fixup_destroy is called when:
1056 * - an active object is destroyed
1057 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001058static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001059{
1060 struct self_test *obj = addr;
1061
1062 switch (state) {
1063 case ODEBUG_STATE_ACTIVE:
1064 debug_object_deactivate(obj, &descr_type_test);
1065 debug_object_destroy(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001066 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001067 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001068 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001069 }
1070}
1071
1072/*
1073 * fixup_free is called when:
1074 * - an active object is freed
1075 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001076static bool __init fixup_free(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001077{
1078 struct self_test *obj = addr;
1079
1080 switch (state) {
1081 case ODEBUG_STATE_ACTIVE:
1082 debug_object_deactivate(obj, &descr_type_test);
1083 debug_object_free(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001084 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001085 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001086 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001087 }
1088}
1089
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001090static int __init
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001091check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
1092{
1093 struct debug_bucket *db;
1094 struct debug_obj *obj;
1095 unsigned long flags;
1096 int res = -EINVAL;
1097
1098 db = get_bucket((unsigned long) addr);
1099
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001100 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001101
1102 obj = lookup_object(addr, db);
1103 if (!obj && state != ODEBUG_STATE_NONE) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001104 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001105 goto out;
1106 }
1107 if (obj && obj->state != state) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001108 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001109 obj->state, state);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001110 goto out;
1111 }
1112 if (fixups != debug_objects_fixups) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001113 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001114 fixups, debug_objects_fixups);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001115 goto out;
1116 }
1117 if (warnings != debug_objects_warnings) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001118 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001119 warnings, debug_objects_warnings);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001120 goto out;
1121 }
1122 res = 0;
1123out:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001124 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001125 if (res)
1126 debug_objects_enabled = 0;
1127 return res;
1128}
1129
1130static __initdata struct debug_obj_descr descr_type_test = {
1131 .name = "selftest",
Du, Changbinb9fdac7f2016-05-19 17:09:41 -07001132 .is_static_object = is_static_object,
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001133 .fixup_init = fixup_init,
1134 .fixup_activate = fixup_activate,
1135 .fixup_destroy = fixup_destroy,
1136 .fixup_free = fixup_free,
1137};
1138
1139static __initdata struct self_test obj = { .static_init = 0 };
1140
1141static void __init debug_objects_selftest(void)
1142{
1143 int fixups, oldfixups, warnings, oldwarnings;
1144 unsigned long flags;
1145
1146 local_irq_save(flags);
1147
1148 fixups = oldfixups = debug_objects_fixups;
1149 warnings = oldwarnings = debug_objects_warnings;
1150 descr_test = &descr_type_test;
1151
1152 debug_object_init(&obj, &descr_type_test);
1153 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1154 goto out;
1155 debug_object_activate(&obj, &descr_type_test);
1156 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1157 goto out;
1158 debug_object_activate(&obj, &descr_type_test);
1159 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1160 goto out;
1161 debug_object_deactivate(&obj, &descr_type_test);
1162 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1163 goto out;
1164 debug_object_destroy(&obj, &descr_type_test);
1165 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1166 goto out;
1167 debug_object_init(&obj, &descr_type_test);
1168 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1169 goto out;
1170 debug_object_activate(&obj, &descr_type_test);
1171 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1172 goto out;
1173 debug_object_deactivate(&obj, &descr_type_test);
1174 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1175 goto out;
1176 debug_object_free(&obj, &descr_type_test);
1177 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1178 goto out;
1179
1180 obj.static_init = 1;
1181 debug_object_activate(&obj, &descr_type_test);
Stephen Boyd9f78ff02012-03-05 14:59:17 -08001182 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001183 goto out;
1184 debug_object_init(&obj, &descr_type_test);
1185 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1186 goto out;
1187 debug_object_free(&obj, &descr_type_test);
1188 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1189 goto out;
1190
1191#ifdef CONFIG_DEBUG_OBJECTS_FREE
1192 debug_object_init(&obj, &descr_type_test);
1193 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1194 goto out;
1195 debug_object_activate(&obj, &descr_type_test);
1196 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1197 goto out;
1198 __debug_check_no_obj_freed(&obj, sizeof(obj));
1199 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1200 goto out;
1201#endif
Fabian Frederick719e4842014-06-04 16:06:04 -07001202 pr_info("selftest passed\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001203
1204out:
1205 debug_objects_fixups = oldfixups;
1206 debug_objects_warnings = oldwarnings;
1207 descr_test = NULL;
1208
1209 local_irq_restore(flags);
1210}
1211#else
1212static inline void debug_objects_selftest(void) { }
1213#endif
1214
1215/*
1216 * Called during early boot to initialize the hash buckets and link
1217 * the static object pool objects into the poll list. After this call
1218 * the object tracker is fully operational.
1219 */
1220void __init debug_objects_early_init(void)
1221{
1222 int i;
1223
1224 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001225 raw_spin_lock_init(&obj_hash[i].lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001226
1227 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1228 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1229}
1230
1231/*
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001232 * Convert the statically allocated objects to dynamic ones:
1233 */
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001234static int __init debug_objects_replace_static_objects(void)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001235{
1236 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001237 struct hlist_node *tmp;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001238 struct debug_obj *obj, *new;
1239 HLIST_HEAD(objects);
1240 int i, cnt = 0;
1241
1242 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1243 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1244 if (!obj)
1245 goto free;
1246 hlist_add_head(&obj->node, &objects);
1247 }
1248
1249 /*
Qian Caia9ee3a62018-12-28 00:32:32 -08001250 * debug_objects_mem_init() is now called early that only one CPU is up
1251 * and interrupts have been disabled, so it is safe to replace the
1252 * active object references.
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001253 */
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001254
1255 /* Remove the statically allocated objects from the pool */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001256 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001257 hlist_del(&obj->node);
1258 /* Move the allocated objects to the pool */
1259 hlist_move_list(&objects, &obj_pool);
1260
1261 /* Replace the active object references */
1262 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1263 hlist_move_list(&db->list, &objects);
1264
Sasha Levinb67bfe02013-02-27 17:06:00 -08001265 hlist_for_each_entry(obj, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001266 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1267 hlist_del(&new->node);
1268 /* copy object data */
1269 *new = *obj;
1270 hlist_add_head(&new->node, &db->list);
1271 cnt++;
1272 }
1273 }
1274
Fabian Frederickc0f35cc2014-06-04 16:06:05 -07001275 pr_debug("%d of %d active objects replaced\n",
1276 cnt, obj_pool_used);
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001277 return 0;
1278free:
Sasha Levinb67bfe02013-02-27 17:06:00 -08001279 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001280 hlist_del(&obj->node);
1281 kmem_cache_free(obj_cache, obj);
1282 }
1283 return -ENOMEM;
1284}
1285
1286/*
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001287 * Called after the kmem_caches are functional to setup a dedicated
1288 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1289 * prevents that the debug code is called on kmem_cache_free() for the
1290 * debug tracker objects to avoid recursive calls.
1291 */
1292void __init debug_objects_mem_init(void)
1293{
Waiman Long634d61f2019-05-20 10:14:47 -04001294 int cpu, extras;
Waiman Longd86998b2019-05-20 10:14:46 -04001295
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001296 if (!debug_objects_enabled)
1297 return;
1298
Waiman Longd86998b2019-05-20 10:14:46 -04001299 /*
1300 * Initialize the percpu object pools
1301 *
1302 * Initialization is not strictly necessary, but was done for
1303 * completeness.
1304 */
1305 for_each_possible_cpu(cpu)
1306 INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool.free_objs, cpu));
1307
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001308 obj_cache = kmem_cache_create("debug_objects_cache",
1309 sizeof (struct debug_obj), 0,
Qian Cai8de456c2018-11-30 14:09:48 -08001310 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
1311 NULL);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001312
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001313 if (!obj_cache || debug_objects_replace_static_objects()) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001314 debug_objects_enabled = 0;
Zhong Jiang3ff4f802018-08-01 00:24:58 +08001315 kmem_cache_destroy(obj_cache);
Fabian Frederick719e4842014-06-04 16:06:04 -07001316 pr_warn("out of memory.\n");
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001317 } else
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001318 debug_objects_selftest();
Waiman Long634d61f2019-05-20 10:14:47 -04001319
1320 /*
1321 * Increase the thresholds for allocating and freeing objects
1322 * according to the number of possible CPUs available in the system.
1323 */
1324 extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
1325 debug_objects_pool_size += extras;
1326 debug_objects_pool_min_level += extras;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001327}