blob: 61261195f5b60b4c143adbca5967f979372798c5 [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
Waiman Longa7344a62019-05-20 10:14:49 -040035/*
36 * We limit the freeing of debug objects via workqueue at a maximum
37 * frequency of 10Hz and about 1024 objects for each freeing operation.
38 * So it is freeing at most 10k debug objects per second.
39 */
40#define ODEBUG_FREE_WORK_MAX 1024
41#define ODEBUG_FREE_WORK_DELAY DIV_ROUND_UP(HZ, 10)
42
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070043struct debug_bucket {
44 struct hlist_head list;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010045 raw_spinlock_t lock;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070046};
47
Waiman Longd86998b2019-05-20 10:14:46 -040048/*
49 * Debug object percpu free list
50 * Access is protected by disabling irq
51 */
52struct debug_percpu_free {
53 struct hlist_head free_objs;
54 int obj_free;
55};
56
57static DEFINE_PER_CPU(struct debug_percpu_free, percpu_obj_pool);
58
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070059static struct debug_bucket obj_hash[ODEBUG_HASH_SIZE];
60
Thomas Gleixner1be1cb72009-03-16 18:53:18 +010061static struct debug_obj obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070062
Thomas Gleixneraef9cb02009-11-17 18:11:28 +010063static DEFINE_RAW_SPINLOCK(pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070064
65static HLIST_HEAD(obj_pool);
Yang Shi36c4ead2018-02-06 07:18:26 +080066static HLIST_HEAD(obj_to_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070067
Waiman Longd86998b2019-05-20 10:14:46 -040068/*
69 * Because of the presence of percpu free pools, obj_pool_free will
70 * under-count those in the percpu free pools. Similarly, obj_pool_used
71 * will over-count those in the percpu free pools. Adjustments will be
72 * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
73 * can be off.
74 */
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070075static int obj_pool_min_free = ODEBUG_POOL_SIZE;
76static int obj_pool_free = ODEBUG_POOL_SIZE;
77static int obj_pool_used;
78static int obj_pool_max_used;
Waiman Longa7344a62019-05-20 10:14:49 -040079static bool obj_freeing;
Yang Shi36c4ead2018-02-06 07:18:26 +080080/* The number of objs on the global free list */
81static int obj_nr_tofree;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070082
83static int debug_objects_maxchain __read_mostly;
Arnd Bergmann163cf842018-03-13 14:18:46 +010084static int __maybe_unused debug_objects_maxchecked __read_mostly;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070085static int debug_objects_fixups __read_mostly;
86static int debug_objects_warnings __read_mostly;
Ingo Molnar3ae70202008-11-26 10:02:00 +010087static int debug_objects_enabled __read_mostly
88 = CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
Waiman Long97dd5522017-01-05 15:17:04 -050089static int debug_objects_pool_size __read_mostly
90 = ODEBUG_POOL_SIZE;
91static int debug_objects_pool_min_level __read_mostly
92 = ODEBUG_POOL_MIN_LEVEL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070093static struct debug_obj_descr *descr_test __read_mostly;
Waiman Longd86998b2019-05-20 10:14:46 -040094static struct kmem_cache *obj_cache __read_mostly;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -070095
Waiman Longc4b73aa2017-01-05 15:17:03 -050096/*
Waiman Long0cad93c2017-02-07 16:40:30 -050097 * Track numbers of kmem_cache_alloc()/free() calls done.
Waiman Longc4b73aa2017-01-05 15:17:03 -050098 */
Waiman Long0cad93c2017-02-07 16:40:30 -050099static int debug_objects_allocated;
Waiman Longc4b73aa2017-01-05 15:17:03 -0500100static int debug_objects_freed;
101
Thomas Gleixner337fff82009-03-16 10:04:53 +0100102static void free_obj_work(struct work_struct *work);
Waiman Longa7344a62019-05-20 10:14:49 -0400103static DECLARE_DELAYED_WORK(debug_obj_work, free_obj_work);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100104
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700105static int __init enable_object_debug(char *str)
106{
107 debug_objects_enabled = 1;
108 return 0;
109}
Kyle McMartin3e8ebb52009-03-01 20:41:41 -0500110
111static int __init disable_object_debug(char *str)
112{
113 debug_objects_enabled = 0;
114 return 0;
115}
116
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700117early_param("debug_objects", enable_object_debug);
Kyle McMartin3e8ebb52009-03-01 20:41:41 -0500118early_param("no_debug_objects", disable_object_debug);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700119
120static const char *obj_states[ODEBUG_STATE_MAX] = {
121 [ODEBUG_STATE_NONE] = "none",
122 [ODEBUG_STATE_INIT] = "initialized",
123 [ODEBUG_STATE_INACTIVE] = "inactive",
124 [ODEBUG_STATE_ACTIVE] = "active",
125 [ODEBUG_STATE_DESTROYED] = "destroyed",
126 [ODEBUG_STATE_NOTAVAILABLE] = "not available",
127};
128
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200129static void fill_pool(void)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700130{
131 gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
Waiman Longd26bf502019-05-20 10:14:48 -0400132 struct debug_obj *obj;
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200133 unsigned long flags;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700134
Waiman Long97dd5522017-01-05 15:17:04 -0500135 if (likely(obj_pool_free >= debug_objects_pool_min_level))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200136 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700137
Yang Shi36c4ead2018-02-06 07:18:26 +0800138 /*
139 * Reuse objs from the global free list; they will be reinitialized
140 * when allocating.
141 */
142 while (obj_nr_tofree && (obj_pool_free < obj_pool_min_free)) {
143 raw_spin_lock_irqsave(&pool_lock, flags);
144 /*
145 * Recheck with the lock held as the worker thread might have
146 * won the race and freed the global free list already.
147 */
Waiman Longd26bf502019-05-20 10:14:48 -0400148 while (obj_nr_tofree && (obj_pool_free < obj_pool_min_free)) {
Yang Shi36c4ead2018-02-06 07:18:26 +0800149 obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
150 hlist_del(&obj->node);
151 obj_nr_tofree--;
152 hlist_add_head(&obj->node, &obj_pool);
153 obj_pool_free++;
154 }
155 raw_spin_unlock_irqrestore(&pool_lock, flags);
156 }
157
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700158 if (unlikely(!obj_cache))
Thomas Gleixner1fda1072012-04-11 11:52:18 +0200159 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700160
Waiman Long97dd5522017-01-05 15:17:04 -0500161 while (obj_pool_free < debug_objects_pool_min_level) {
Waiman Longd26bf502019-05-20 10:14:48 -0400162 struct debug_obj *new[ODEBUG_BATCH_SIZE];
163 int cnt;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700164
Waiman Longd26bf502019-05-20 10:14:48 -0400165 for (cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) {
166 new[cnt] = kmem_cache_zalloc(obj_cache, gfp);
167 if (!new[cnt])
168 break;
169 }
170 if (!cnt)
Dan Carpenter33408082012-04-18 14:28:10 +0300171 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700172
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100173 raw_spin_lock_irqsave(&pool_lock, flags);
Waiman Longd26bf502019-05-20 10:14:48 -0400174 while (cnt) {
175 hlist_add_head(&new[--cnt]->node, &obj_pool);
176 debug_objects_allocated++;
177 obj_pool_free++;
178 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100179 raw_spin_unlock_irqrestore(&pool_lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700180 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700181}
182
183/*
184 * Lookup an object in the hash bucket.
185 */
186static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
187{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700188 struct debug_obj *obj;
189 int cnt = 0;
190
Sasha Levinb67bfe02013-02-27 17:06:00 -0800191 hlist_for_each_entry(obj, &b->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700192 cnt++;
193 if (obj->object == addr)
194 return obj;
195 }
196 if (cnt > debug_objects_maxchain)
197 debug_objects_maxchain = cnt;
198
199 return NULL;
200}
201
202/*
Waiman Longd86998b2019-05-20 10:14:46 -0400203 * Allocate a new object from the hlist
204 */
205static struct debug_obj *__alloc_object(struct hlist_head *list)
206{
207 struct debug_obj *obj = NULL;
208
209 if (list->first) {
210 obj = hlist_entry(list->first, typeof(*obj), node);
211 hlist_del(&obj->node);
212 }
213
214 return obj;
215}
216
217/*
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200218 * Allocate a new object. If the pool is empty, switch off the debugger.
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200219 * Must be called with interrupts disabled.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700220 */
221static struct debug_obj *
222alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
223{
Waiman Long634d61f2019-05-20 10:14:47 -0400224 struct debug_percpu_free *percpu_pool = this_cpu_ptr(&percpu_obj_pool);
Waiman Longd86998b2019-05-20 10:14:46 -0400225 struct debug_obj *obj;
226
227 if (likely(obj_cache)) {
Waiman Longd86998b2019-05-20 10:14:46 -0400228 obj = __alloc_object(&percpu_pool->free_objs);
229 if (obj) {
230 percpu_pool->obj_free--;
231 goto init_obj;
232 }
233 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700234
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100235 raw_spin_lock(&pool_lock);
Waiman Longd86998b2019-05-20 10:14:46 -0400236 obj = __alloc_object(&obj_pool);
237 if (obj) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700238 obj_pool_used++;
Waiman Long634d61f2019-05-20 10:14:47 -0400239 obj_pool_free--;
240
241 /*
242 * Looking ahead, allocate one batch of debug objects and
243 * put them into the percpu free pool.
244 */
245 if (likely(obj_cache)) {
246 int i;
247
248 for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
249 struct debug_obj *obj2;
250
251 obj2 = __alloc_object(&obj_pool);
252 if (!obj2)
253 break;
254 hlist_add_head(&obj2->node,
255 &percpu_pool->free_objs);
256 percpu_pool->obj_free++;
257 obj_pool_used++;
258 obj_pool_free--;
259 }
260 }
261
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700262 if (obj_pool_used > obj_pool_max_used)
263 obj_pool_max_used = obj_pool_used;
264
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700265 if (obj_pool_free < obj_pool_min_free)
266 obj_pool_min_free = obj_pool_free;
267 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100268 raw_spin_unlock(&pool_lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700269
Waiman Longd86998b2019-05-20 10:14:46 -0400270init_obj:
271 if (obj) {
272 obj->object = addr;
273 obj->descr = descr;
274 obj->state = ODEBUG_STATE_NONE;
275 obj->astate = 0;
276 hlist_add_head(&obj->node, &b->list);
277 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700278 return obj;
279}
280
281/*
Thomas Gleixner337fff82009-03-16 10:04:53 +0100282 * workqueue function to free objects.
Waiman Long858274b2017-01-05 15:17:05 -0500283 *
284 * To reduce contention on the global pool_lock, the actual freeing of
Yang Shi636e1972018-02-06 07:18:27 +0800285 * debug objects will be delayed if the pool_lock is busy.
Thomas Gleixner337fff82009-03-16 10:04:53 +0100286 */
287static void free_obj_work(struct work_struct *work)
288{
Yang Shi36c4ead2018-02-06 07:18:26 +0800289 struct hlist_node *tmp;
290 struct debug_obj *obj;
Thomas Gleixner337fff82009-03-16 10:04:53 +0100291 unsigned long flags;
Yang Shi36c4ead2018-02-06 07:18:26 +0800292 HLIST_HEAD(tofree);
Thomas Gleixner337fff82009-03-16 10:04:53 +0100293
Waiman Longa7344a62019-05-20 10:14:49 -0400294 WRITE_ONCE(obj_freeing, false);
Waiman Long858274b2017-01-05 15:17:05 -0500295 if (!raw_spin_trylock_irqsave(&pool_lock, flags))
296 return;
Yang Shi36c4ead2018-02-06 07:18:26 +0800297
Waiman Longa7344a62019-05-20 10:14:49 -0400298 if (obj_pool_free >= debug_objects_pool_size)
299 goto free_objs;
300
Yang Shi36c4ead2018-02-06 07:18:26 +0800301 /*
302 * The objs on the pool list might be allocated before the work is
303 * run, so recheck if pool list it full or not, if not fill pool
Waiman Longa7344a62019-05-20 10:14:49 -0400304 * list from the global free list. As it is likely that a workload
305 * may be gearing up to use more and more objects, don't free any
306 * of them until the next round.
Yang Shi36c4ead2018-02-06 07:18:26 +0800307 */
308 while (obj_nr_tofree && obj_pool_free < debug_objects_pool_size) {
309 obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
310 hlist_del(&obj->node);
311 hlist_add_head(&obj->node, &obj_pool);
312 obj_pool_free++;
313 obj_nr_tofree--;
314 }
Waiman Longa7344a62019-05-20 10:14:49 -0400315 raw_spin_unlock_irqrestore(&pool_lock, flags);
316 return;
Yang Shi36c4ead2018-02-06 07:18:26 +0800317
Waiman Longa7344a62019-05-20 10:14:49 -0400318free_objs:
Yang Shi36c4ead2018-02-06 07:18:26 +0800319 /*
320 * Pool list is already full and there are still objs on the free
321 * list. Move remaining free objs to a temporary list to free the
322 * memory outside the pool_lock held region.
323 */
324 if (obj_nr_tofree) {
325 hlist_move_list(&obj_to_free, &tofree);
Arnd Bergmann04148182018-02-22 16:52:58 +0100326 debug_objects_freed += obj_nr_tofree;
Yang Shi36c4ead2018-02-06 07:18:26 +0800327 obj_nr_tofree = 0;
328 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100329 raw_spin_unlock_irqrestore(&pool_lock, flags);
Yang Shi36c4ead2018-02-06 07:18:26 +0800330
331 hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
332 hlist_del(&obj->node);
333 kmem_cache_free(obj_cache, obj);
334 }
Thomas Gleixner337fff82009-03-16 10:04:53 +0100335}
336
Waiman Longa7344a62019-05-20 10:14:49 -0400337static void __free_object(struct debug_obj *obj)
Yang Shi636e1972018-02-06 07:18:27 +0800338{
Waiman Long634d61f2019-05-20 10:14:47 -0400339 struct debug_obj *objs[ODEBUG_BATCH_SIZE];
340 struct debug_percpu_free *percpu_pool;
341 int lookahead_count = 0;
Yang Shi636e1972018-02-06 07:18:27 +0800342 unsigned long flags;
343 bool work;
344
Waiman Longd86998b2019-05-20 10:14:46 -0400345 local_irq_save(flags);
Waiman Long634d61f2019-05-20 10:14:47 -0400346 if (!obj_cache)
347 goto free_to_obj_pool;
348
Waiman Longd86998b2019-05-20 10:14:46 -0400349 /*
350 * Try to free it into the percpu pool first.
351 */
352 percpu_pool = this_cpu_ptr(&percpu_obj_pool);
Waiman Long634d61f2019-05-20 10:14:47 -0400353 if (percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) {
Waiman Longd86998b2019-05-20 10:14:46 -0400354 hlist_add_head(&obj->node, &percpu_pool->free_objs);
355 percpu_pool->obj_free++;
356 local_irq_restore(flags);
Waiman Longa7344a62019-05-20 10:14:49 -0400357 return;
Waiman Longd86998b2019-05-20 10:14:46 -0400358 }
359
Waiman Long634d61f2019-05-20 10:14:47 -0400360 /*
361 * As the percpu pool is full, look ahead and pull out a batch
362 * of objects from the percpu pool and free them as well.
363 */
364 for (; lookahead_count < ODEBUG_BATCH_SIZE; lookahead_count++) {
365 objs[lookahead_count] = __alloc_object(&percpu_pool->free_objs);
366 if (!objs[lookahead_count])
367 break;
368 percpu_pool->obj_free--;
369 }
370
371free_to_obj_pool:
Waiman Longd86998b2019-05-20 10:14:46 -0400372 raw_spin_lock(&pool_lock);
Waiman Longa7344a62019-05-20 10:14:49 -0400373 work = (obj_pool_free > debug_objects_pool_size) && obj_cache &&
374 (obj_nr_tofree < ODEBUG_FREE_WORK_MAX);
Yang Shi636e1972018-02-06 07:18:27 +0800375 obj_pool_used--;
376
377 if (work) {
378 obj_nr_tofree++;
379 hlist_add_head(&obj->node, &obj_to_free);
Waiman Long634d61f2019-05-20 10:14:47 -0400380 if (lookahead_count) {
381 obj_nr_tofree += lookahead_count;
382 obj_pool_used -= lookahead_count;
383 while (lookahead_count) {
384 hlist_add_head(&objs[--lookahead_count]->node,
385 &obj_to_free);
386 }
387 }
Waiman Longa7344a62019-05-20 10:14:49 -0400388
389 if ((obj_pool_free > debug_objects_pool_size) &&
390 (obj_nr_tofree < ODEBUG_FREE_WORK_MAX)) {
391 int i;
392
393 /*
394 * Free one more batch of objects from obj_pool.
395 */
396 for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
397 obj = __alloc_object(&obj_pool);
398 hlist_add_head(&obj->node, &obj_to_free);
399 obj_pool_free--;
400 obj_nr_tofree++;
401 }
402 }
Yang Shi636e1972018-02-06 07:18:27 +0800403 } else {
404 obj_pool_free++;
405 hlist_add_head(&obj->node, &obj_pool);
Waiman Long634d61f2019-05-20 10:14:47 -0400406 if (lookahead_count) {
407 obj_pool_free += lookahead_count;
408 obj_pool_used -= lookahead_count;
409 while (lookahead_count) {
410 hlist_add_head(&objs[--lookahead_count]->node,
411 &obj_pool);
412 }
413 }
Yang Shi636e1972018-02-06 07:18:27 +0800414 }
Waiman Longd86998b2019-05-20 10:14:46 -0400415 raw_spin_unlock(&pool_lock);
416 local_irq_restore(flags);
Yang Shi636e1972018-02-06 07:18:27 +0800417}
418
Thomas Gleixner337fff82009-03-16 10:04:53 +0100419/*
420 * Put the object back into the pool and schedule work to free objects
421 * if necessary.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700422 */
423static void free_object(struct debug_obj *obj)
424{
Waiman Longa7344a62019-05-20 10:14:49 -0400425 __free_object(obj);
426 if (!obj_freeing && obj_nr_tofree) {
427 WRITE_ONCE(obj_freeing, true);
428 schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
429 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700430}
431
432/*
433 * We run out of memory. That means we probably have tons of objects
434 * allocated.
435 */
436static void debug_objects_oom(void)
437{
438 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800439 struct hlist_node *tmp;
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200440 HLIST_HEAD(freelist);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700441 struct debug_obj *obj;
442 unsigned long flags;
443 int i;
444
Fabian Frederick719e4842014-06-04 16:06:04 -0700445 pr_warn("Out of memory. ODEBUG disabled\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700446
447 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100448 raw_spin_lock_irqsave(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200449 hlist_move_list(&db->list, &freelist);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100450 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200451
452 /* Now free them */
Sasha Levinb67bfe02013-02-27 17:06:00 -0800453 hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700454 hlist_del(&obj->node);
455 free_object(obj);
456 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700457 }
458}
459
460/*
461 * We use the pfn of the address for the hash. That way we can check
462 * for freed objects simply by checking the affected bucket.
463 */
464static struct debug_bucket *get_bucket(unsigned long addr)
465{
466 unsigned long hash;
467
468 hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
469 return &obj_hash[hash];
470}
471
472static void debug_print_object(struct debug_obj *obj, char *msg)
473{
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100474 struct debug_obj_descr *descr = obj->descr;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700475 static int limit;
476
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100477 if (limit < 5 && descr != descr_test) {
478 void *hint = descr->debug_hint ?
479 descr->debug_hint(obj->object) : NULL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700480 limit++;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400481 WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100482 "object type: %s hint: %pS\n",
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400483 msg, obj_states[obj->state], obj->astate,
Stanislaw Gruszka99777282011-03-07 09:58:33 +0100484 descr->name, hint);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700485 }
486 debug_objects_warnings++;
487}
488
489/*
490 * Try to repair the damage, so we have a better chance to get useful
491 * debug output.
492 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700493static bool
494debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700495 void * addr, enum debug_obj_state state)
496{
Du, Changbinb1e4d9d2016-05-19 17:09:20 -0700497 if (fixup && fixup(addr, state)) {
498 debug_objects_fixups++;
499 return true;
500 }
501 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700502}
503
504static void debug_object_is_on_stack(void *addr, int onstack)
505{
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700506 int is_on_stack;
507 static int limit;
508
509 if (limit > 4)
510 return;
511
FUJITA Tomonori8b05c7e2008-07-23 21:26:53 -0700512 is_on_stack = object_is_on_stack(addr);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700513 if (is_on_stack == onstack)
514 return;
515
516 limit++;
517 if (is_on_stack)
Joel Fernandes (Google)fc91a3c2018-07-23 14:25:31 -0700518 pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
519 task_stack_page(current));
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700520 else
Joel Fernandes (Google)fc91a3c2018-07-23 14:25:31 -0700521 pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
522 task_stack_page(current));
523
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700524 WARN_ON(1);
525}
526
527static void
528__debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
529{
530 enum debug_obj_state state;
Waiman Longd5f34152019-05-20 10:14:50 -0400531 bool check_stack = false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700532 struct debug_bucket *db;
533 struct debug_obj *obj;
534 unsigned long flags;
535
Vegard Nossum50db04dd2008-06-15 00:47:36 +0200536 fill_pool();
537
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700538 db = get_bucket((unsigned long) addr);
539
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100540 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700541
542 obj = lookup_object(addr, db);
543 if (!obj) {
544 obj = alloc_object(addr, db, descr);
545 if (!obj) {
546 debug_objects_enabled = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100547 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700548 debug_objects_oom();
549 return;
550 }
Waiman Longd5f34152019-05-20 10:14:50 -0400551 check_stack = true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700552 }
553
554 switch (obj->state) {
555 case ODEBUG_STATE_NONE:
556 case ODEBUG_STATE_INIT:
557 case ODEBUG_STATE_INACTIVE:
558 obj->state = ODEBUG_STATE_INIT;
559 break;
560
561 case ODEBUG_STATE_ACTIVE:
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700562 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100563 raw_spin_unlock_irqrestore(&db->lock, flags);
Waiman Longd5f34152019-05-20 10:14:50 -0400564 debug_print_object(obj, "init");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700565 debug_object_fixup(descr->fixup_init, addr, state);
566 return;
567
568 case ODEBUG_STATE_DESTROYED:
Waiman Longd5f34152019-05-20 10:14:50 -0400569 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700570 debug_print_object(obj, "init");
Waiman Longd5f34152019-05-20 10:14:50 -0400571 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700572 default:
573 break;
574 }
575
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100576 raw_spin_unlock_irqrestore(&db->lock, flags);
Waiman Longd5f34152019-05-20 10:14:50 -0400577 if (check_stack)
578 debug_object_is_on_stack(addr, onstack);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700579}
580
581/**
582 * debug_object_init - debug checks when an object is initialized
583 * @addr: address of the object
584 * @descr: pointer to an object specific debug description structure
585 */
586void debug_object_init(void *addr, struct debug_obj_descr *descr)
587{
588 if (!debug_objects_enabled)
589 return;
590
591 __debug_object_init(addr, descr, 0);
592}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800593EXPORT_SYMBOL_GPL(debug_object_init);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700594
595/**
596 * debug_object_init_on_stack - debug checks when an object on stack is
597 * initialized
598 * @addr: address of the object
599 * @descr: pointer to an object specific debug description structure
600 */
601void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
602{
603 if (!debug_objects_enabled)
604 return;
605
606 __debug_object_init(addr, descr, 1);
607}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800608EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700609
610/**
611 * debug_object_activate - debug checks when an object is activated
612 * @addr: address of the object
613 * @descr: pointer to an object specific debug description structure
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700614 * Returns 0 for success, -EINVAL for check failed.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700615 */
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700616int debug_object_activate(void *addr, struct debug_obj_descr *descr)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700617{
618 enum debug_obj_state state;
619 struct debug_bucket *db;
620 struct debug_obj *obj;
621 unsigned long flags;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700622 int ret;
Stephen Boydfeac18d2011-11-07 19:48:26 -0800623 struct debug_obj o = { .object = addr,
624 .state = ODEBUG_STATE_NOTAVAILABLE,
625 .descr = descr };
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700626
627 if (!debug_objects_enabled)
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700628 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700629
630 db = get_bucket((unsigned long) addr);
631
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100632 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700633
634 obj = lookup_object(addr, db);
635 if (obj) {
Waiman Longd5f34152019-05-20 10:14:50 -0400636 bool print_object = false;
637
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700638 switch (obj->state) {
639 case ODEBUG_STATE_INIT:
640 case ODEBUG_STATE_INACTIVE:
641 obj->state = ODEBUG_STATE_ACTIVE;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700642 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700643 break;
644
645 case ODEBUG_STATE_ACTIVE:
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700646 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100647 raw_spin_unlock_irqrestore(&db->lock, flags);
Waiman Longd5f34152019-05-20 10:14:50 -0400648 debug_print_object(obj, "activate");
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700649 ret = debug_object_fixup(descr->fixup_activate, addr, state);
Du, Changbine7a8e782016-05-19 17:09:23 -0700650 return ret ? 0 : -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700651
652 case ODEBUG_STATE_DESTROYED:
Waiman Longd5f34152019-05-20 10:14:50 -0400653 print_object = true;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700654 ret = -EINVAL;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700655 break;
656 default:
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700657 ret = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700658 break;
659 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100660 raw_spin_unlock_irqrestore(&db->lock, flags);
Waiman Longd5f34152019-05-20 10:14:50 -0400661 if (print_object)
662 debug_print_object(obj, "activate");
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700663 return ret;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700664 }
665
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100666 raw_spin_unlock_irqrestore(&db->lock, flags);
Waiman Longd5f34152019-05-20 10:14:50 -0400667
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700668 /*
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700669 * We are here when a static object is activated. We
670 * let the type specific code confirm whether this is
671 * true or not. if true, we just make sure that the
672 * static object is tracked in the object tracker. If
673 * not, this must be a bug, so we try to fix it up.
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700674 */
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700675 if (descr->is_static_object && descr->is_static_object(addr)) {
676 /* track this static object */
677 debug_object_init(addr, descr);
678 debug_object_activate(addr, descr);
679 } else {
Stephen Boydfeac18d2011-11-07 19:48:26 -0800680 debug_print_object(&o, "activate");
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700681 ret = debug_object_fixup(descr->fixup_activate, addr,
682 ODEBUG_STATE_NOTAVAILABLE);
683 return ret ? 0 : -EINVAL;
Paul E. McKenneyb778ae22013-04-23 12:51:11 -0700684 }
685 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700686}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800687EXPORT_SYMBOL_GPL(debug_object_activate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700688
689/**
690 * debug_object_deactivate - debug checks when an object is deactivated
691 * @addr: address of the object
692 * @descr: pointer to an object specific debug description structure
693 */
694void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
695{
696 struct debug_bucket *db;
697 struct debug_obj *obj;
698 unsigned long flags;
Waiman Longd5f34152019-05-20 10:14:50 -0400699 bool print_object = false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700700
701 if (!debug_objects_enabled)
702 return;
703
704 db = get_bucket((unsigned long) addr);
705
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100706 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700707
708 obj = lookup_object(addr, db);
709 if (obj) {
710 switch (obj->state) {
711 case ODEBUG_STATE_INIT:
712 case ODEBUG_STATE_INACTIVE:
713 case ODEBUG_STATE_ACTIVE:
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400714 if (!obj->astate)
715 obj->state = ODEBUG_STATE_INACTIVE;
716 else
Waiman Longd5f34152019-05-20 10:14:50 -0400717 print_object = true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700718 break;
719
720 case ODEBUG_STATE_DESTROYED:
Waiman Longd5f34152019-05-20 10:14:50 -0400721 print_object = true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700722 break;
723 default:
724 break;
725 }
Waiman Longd5f34152019-05-20 10:14:50 -0400726 }
727
728 raw_spin_unlock_irqrestore(&db->lock, flags);
729 if (!obj) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700730 struct debug_obj o = { .object = addr,
731 .state = ODEBUG_STATE_NOTAVAILABLE,
732 .descr = descr };
733
734 debug_print_object(&o, "deactivate");
Waiman Longd5f34152019-05-20 10:14:50 -0400735 } else if (print_object) {
736 debug_print_object(obj, "deactivate");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700737 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700738}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800739EXPORT_SYMBOL_GPL(debug_object_deactivate);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700740
741/**
742 * debug_object_destroy - debug checks when an object is destroyed
743 * @addr: address of the object
744 * @descr: pointer to an object specific debug description structure
745 */
746void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
747{
748 enum debug_obj_state state;
749 struct debug_bucket *db;
750 struct debug_obj *obj;
751 unsigned long flags;
Waiman Longd5f34152019-05-20 10:14:50 -0400752 bool print_object = false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700753
754 if (!debug_objects_enabled)
755 return;
756
757 db = get_bucket((unsigned long) addr);
758
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100759 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700760
761 obj = lookup_object(addr, db);
762 if (!obj)
763 goto out_unlock;
764
765 switch (obj->state) {
766 case ODEBUG_STATE_NONE:
767 case ODEBUG_STATE_INIT:
768 case ODEBUG_STATE_INACTIVE:
769 obj->state = ODEBUG_STATE_DESTROYED;
770 break;
771 case ODEBUG_STATE_ACTIVE:
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700772 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100773 raw_spin_unlock_irqrestore(&db->lock, flags);
Waiman Longd5f34152019-05-20 10:14:50 -0400774 debug_print_object(obj, "destroy");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700775 debug_object_fixup(descr->fixup_destroy, addr, state);
776 return;
777
778 case ODEBUG_STATE_DESTROYED:
Waiman Longd5f34152019-05-20 10:14:50 -0400779 print_object = true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700780 break;
781 default:
782 break;
783 }
784out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100785 raw_spin_unlock_irqrestore(&db->lock, flags);
Waiman Longd5f34152019-05-20 10:14:50 -0400786 if (print_object)
787 debug_print_object(obj, "destroy");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700788}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800789EXPORT_SYMBOL_GPL(debug_object_destroy);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700790
791/**
792 * debug_object_free - debug checks when an object is freed
793 * @addr: address of the object
794 * @descr: pointer to an object specific debug description structure
795 */
796void debug_object_free(void *addr, struct debug_obj_descr *descr)
797{
798 enum debug_obj_state state;
799 struct debug_bucket *db;
800 struct debug_obj *obj;
801 unsigned long flags;
802
803 if (!debug_objects_enabled)
804 return;
805
806 db = get_bucket((unsigned long) addr);
807
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100808 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700809
810 obj = lookup_object(addr, db);
811 if (!obj)
812 goto out_unlock;
813
814 switch (obj->state) {
815 case ODEBUG_STATE_ACTIVE:
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700816 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100817 raw_spin_unlock_irqrestore(&db->lock, flags);
Waiman Longd5f34152019-05-20 10:14:50 -0400818 debug_print_object(obj, "free");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700819 debug_object_fixup(descr->fixup_free, addr, state);
820 return;
821 default:
822 hlist_del(&obj->node);
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100823 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700824 free_object(obj);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200825 return;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700826 }
827out_unlock:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100828 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700829}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800830EXPORT_SYMBOL_GPL(debug_object_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700831
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400832/**
Christine Chanb84d4352011-11-07 19:48:27 -0800833 * debug_object_assert_init - debug checks when object should be init-ed
834 * @addr: address of the object
835 * @descr: pointer to an object specific debug description structure
836 */
837void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
838{
839 struct debug_bucket *db;
840 struct debug_obj *obj;
841 unsigned long flags;
842
843 if (!debug_objects_enabled)
844 return;
845
846 db = get_bucket((unsigned long) addr);
847
848 raw_spin_lock_irqsave(&db->lock, flags);
849
850 obj = lookup_object(addr, db);
851 if (!obj) {
852 struct debug_obj o = { .object = addr,
853 .state = ODEBUG_STATE_NOTAVAILABLE,
854 .descr = descr };
855
856 raw_spin_unlock_irqrestore(&db->lock, flags);
857 /*
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700858 * Maybe the object is static, and we let the type specific
859 * code confirm. Track this static object if true, else invoke
860 * fixup.
Christine Chanb84d4352011-11-07 19:48:27 -0800861 */
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700862 if (descr->is_static_object && descr->is_static_object(addr)) {
863 /* Track this static object */
864 debug_object_init(addr, descr);
865 } else {
Christine Chanb84d4352011-11-07 19:48:27 -0800866 debug_print_object(&o, "assert_init");
Du, Changbinb9fdac7f2016-05-19 17:09:41 -0700867 debug_object_fixup(descr->fixup_assert_init, addr,
868 ODEBUG_STATE_NOTAVAILABLE);
869 }
Christine Chanb84d4352011-11-07 19:48:27 -0800870 return;
871 }
872
873 raw_spin_unlock_irqrestore(&db->lock, flags);
874}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800875EXPORT_SYMBOL_GPL(debug_object_assert_init);
Christine Chanb84d4352011-11-07 19:48:27 -0800876
877/**
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400878 * debug_object_active_state - debug checks object usage state machine
879 * @addr: address of the object
880 * @descr: pointer to an object specific debug description structure
881 * @expect: expected state
882 * @next: state to move to if expected state is found
883 */
884void
885debug_object_active_state(void *addr, struct debug_obj_descr *descr,
886 unsigned int expect, unsigned int next)
887{
888 struct debug_bucket *db;
889 struct debug_obj *obj;
890 unsigned long flags;
Waiman Longd5f34152019-05-20 10:14:50 -0400891 bool print_object = false;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400892
893 if (!debug_objects_enabled)
894 return;
895
896 db = get_bucket((unsigned long) addr);
897
898 raw_spin_lock_irqsave(&db->lock, flags);
899
900 obj = lookup_object(addr, db);
901 if (obj) {
902 switch (obj->state) {
903 case ODEBUG_STATE_ACTIVE:
904 if (obj->astate == expect)
905 obj->astate = next;
906 else
Waiman Longd5f34152019-05-20 10:14:50 -0400907 print_object = true;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400908 break;
909
910 default:
Waiman Longd5f34152019-05-20 10:14:50 -0400911 print_object = true;
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400912 break;
913 }
Waiman Longd5f34152019-05-20 10:14:50 -0400914 }
915
916 raw_spin_unlock_irqrestore(&db->lock, flags);
917 if (!obj) {
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400918 struct debug_obj o = { .object = addr,
919 .state = ODEBUG_STATE_NOTAVAILABLE,
920 .descr = descr };
921
922 debug_print_object(&o, "active_state");
Waiman Longd5f34152019-05-20 10:14:50 -0400923 } else if (print_object) {
924 debug_print_object(obj, "active_state");
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400925 }
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400926}
Chris Wilsonf8ff04e2016-11-30 15:54:10 -0800927EXPORT_SYMBOL_GPL(debug_object_active_state);
Mathieu Desnoyersa5d8e462010-04-17 08:48:38 -0400928
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700929#ifdef CONFIG_DEBUG_OBJECTS_FREE
930static void __debug_check_no_obj_freed(const void *address, unsigned long size)
931{
932 unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700933 struct debug_obj_descr *descr;
934 enum debug_obj_state state;
935 struct debug_bucket *db;
Yang Shi1ea9b982018-02-06 07:18:28 +0800936 struct hlist_node *tmp;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700937 struct debug_obj *obj;
Yang Shibd9dcd02018-02-06 07:18:25 +0800938 int cnt, objs_checked = 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700939
940 saddr = (unsigned long) address;
941 eaddr = saddr + size;
942 paddr = saddr & ODEBUG_CHUNK_MASK;
943 chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
944 chunks >>= ODEBUG_CHUNK_SHIFT;
945
946 for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
947 db = get_bucket(paddr);
948
949repeat:
950 cnt = 0;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100951 raw_spin_lock_irqsave(&db->lock, flags);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800952 hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700953 cnt++;
954 oaddr = (unsigned long) obj->object;
955 if (oaddr < saddr || oaddr >= eaddr)
956 continue;
957
958 switch (obj->state) {
959 case ODEBUG_STATE_ACTIVE:
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700960 descr = obj->descr;
961 state = obj->state;
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100962 raw_spin_unlock_irqrestore(&db->lock, flags);
Waiman Longd5f34152019-05-20 10:14:50 -0400963 debug_print_object(obj, "free");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700964 debug_object_fixup(descr->fixup_free,
965 (void *) oaddr, state);
966 goto repeat;
967 default:
968 hlist_del(&obj->node);
Waiman Longa7344a62019-05-20 10:14:49 -0400969 __free_object(obj);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700970 break;
971 }
972 }
Thomas Gleixneraef9cb02009-11-17 18:11:28 +0100973 raw_spin_unlock_irqrestore(&db->lock, flags);
Vegard Nossum673d62cc2008-08-31 23:39:21 +0200974
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700975 if (cnt > debug_objects_maxchain)
976 debug_objects_maxchain = cnt;
Yang Shibd9dcd02018-02-06 07:18:25 +0800977
978 objs_checked += cnt;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700979 }
Yang Shibd9dcd02018-02-06 07:18:25 +0800980
981 if (objs_checked > debug_objects_maxchecked)
982 debug_objects_maxchecked = objs_checked;
Yang Shi1ea9b982018-02-06 07:18:28 +0800983
984 /* Schedule work to actually kmem_cache_free() objects */
Waiman Longa7344a62019-05-20 10:14:49 -0400985 if (!obj_freeing && obj_nr_tofree) {
986 WRITE_ONCE(obj_freeing, true);
987 schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
988 }
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700989}
990
991void debug_check_no_obj_freed(const void *address, unsigned long size)
992{
993 if (debug_objects_enabled)
994 __debug_check_no_obj_freed(address, size);
995}
996#endif
997
998#ifdef CONFIG_DEBUG_FS
999
1000static int debug_stats_show(struct seq_file *m, void *v)
1001{
Waiman Longd86998b2019-05-20 10:14:46 -04001002 int cpu, obj_percpu_free = 0;
1003
1004 for_each_possible_cpu(cpu)
1005 obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu);
1006
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001007 seq_printf(m, "max_chain :%d\n", debug_objects_maxchain);
Yang Shibd9dcd02018-02-06 07:18:25 +08001008 seq_printf(m, "max_checked :%d\n", debug_objects_maxchecked);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001009 seq_printf(m, "warnings :%d\n", debug_objects_warnings);
1010 seq_printf(m, "fixups :%d\n", debug_objects_fixups);
Waiman Longd86998b2019-05-20 10:14:46 -04001011 seq_printf(m, "pool_free :%d\n", obj_pool_free + obj_percpu_free);
1012 seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001013 seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
Waiman Longd86998b2019-05-20 10:14:46 -04001014 seq_printf(m, "pool_used :%d\n", obj_pool_used - obj_percpu_free);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001015 seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
Yang Shi36c4ead2018-02-06 07:18:26 +08001016 seq_printf(m, "on_free_list :%d\n", obj_nr_tofree);
Waiman Long0cad93c2017-02-07 16:40:30 -05001017 seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
1018 seq_printf(m, "objs_freed :%d\n", debug_objects_freed);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001019 return 0;
1020}
1021
1022static int debug_stats_open(struct inode *inode, struct file *filp)
1023{
1024 return single_open(filp, debug_stats_show, NULL);
1025}
1026
1027static const struct file_operations debug_stats_fops = {
1028 .open = debug_stats_open,
1029 .read = seq_read,
1030 .llseek = seq_lseek,
1031 .release = single_release,
1032};
1033
1034static int __init debug_objects_init_debugfs(void)
1035{
Greg Kroah-Hartmanfecb0d92019-06-12 17:35:13 +02001036 struct dentry *dbgdir;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001037
1038 if (!debug_objects_enabled)
1039 return 0;
1040
1041 dbgdir = debugfs_create_dir("debug_objects", NULL);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001042
Greg Kroah-Hartmanfecb0d92019-06-12 17:35:13 +02001043 debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001044
1045 return 0;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001046}
1047__initcall(debug_objects_init_debugfs);
1048
1049#else
1050static inline void debug_objects_init_debugfs(void) { }
1051#endif
1052
1053#ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
1054
1055/* Random data structure for the self test */
1056struct self_test {
1057 unsigned long dummy1[6];
1058 int static_init;
1059 unsigned long dummy2[3];
1060};
1061
1062static __initdata struct debug_obj_descr descr_type_test;
1063
Du, Changbinb9fdac7f2016-05-19 17:09:41 -07001064static bool __init is_static_object(void *addr)
1065{
1066 struct self_test *obj = addr;
1067
1068 return obj->static_init;
1069}
1070
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001071/*
1072 * fixup_init is called when:
1073 * - an active object is initialized
1074 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001075static bool __init fixup_init(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001076{
1077 struct self_test *obj = addr;
1078
1079 switch (state) {
1080 case ODEBUG_STATE_ACTIVE:
1081 debug_object_deactivate(obj, &descr_type_test);
1082 debug_object_init(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001083 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001084 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001085 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001086 }
1087}
1088
1089/*
1090 * fixup_activate is called when:
1091 * - an active object is activated
Du, Changbinb9fdac7f2016-05-19 17:09:41 -07001092 * - an unknown non-static object is activated
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001093 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001094static bool __init fixup_activate(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001095{
1096 struct self_test *obj = addr;
1097
1098 switch (state) {
1099 case ODEBUG_STATE_NOTAVAILABLE:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001100 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001101 case ODEBUG_STATE_ACTIVE:
1102 debug_object_deactivate(obj, &descr_type_test);
1103 debug_object_activate(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001104 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001105
1106 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001107 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001108 }
1109}
1110
1111/*
1112 * fixup_destroy is called when:
1113 * - an active object is destroyed
1114 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001115static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001116{
1117 struct self_test *obj = addr;
1118
1119 switch (state) {
1120 case ODEBUG_STATE_ACTIVE:
1121 debug_object_deactivate(obj, &descr_type_test);
1122 debug_object_destroy(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001123 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001124 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001125 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001126 }
1127}
1128
1129/*
1130 * fixup_free is called when:
1131 * - an active object is freed
1132 */
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001133static bool __init fixup_free(void *addr, enum debug_obj_state state)
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001134{
1135 struct self_test *obj = addr;
1136
1137 switch (state) {
1138 case ODEBUG_STATE_ACTIVE:
1139 debug_object_deactivate(obj, &descr_type_test);
1140 debug_object_free(obj, &descr_type_test);
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001141 return true;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001142 default:
Du, Changbinb1e4d9d2016-05-19 17:09:20 -07001143 return false;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001144 }
1145}
1146
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001147static int __init
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001148check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
1149{
1150 struct debug_bucket *db;
1151 struct debug_obj *obj;
1152 unsigned long flags;
1153 int res = -EINVAL;
1154
1155 db = get_bucket((unsigned long) addr);
1156
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001157 raw_spin_lock_irqsave(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001158
1159 obj = lookup_object(addr, db);
1160 if (!obj && state != ODEBUG_STATE_NONE) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001161 WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001162 goto out;
1163 }
1164 if (obj && obj->state != state) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001165 WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001166 obj->state, state);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001167 goto out;
1168 }
1169 if (fixups != debug_objects_fixups) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001170 WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001171 fixups, debug_objects_fixups);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001172 goto out;
1173 }
1174 if (warnings != debug_objects_warnings) {
Arjan van de Ven5cd2b452008-07-25 19:45:39 -07001175 WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001176 warnings, debug_objects_warnings);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001177 goto out;
1178 }
1179 res = 0;
1180out:
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001181 raw_spin_unlock_irqrestore(&db->lock, flags);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001182 if (res)
1183 debug_objects_enabled = 0;
1184 return res;
1185}
1186
1187static __initdata struct debug_obj_descr descr_type_test = {
1188 .name = "selftest",
Du, Changbinb9fdac7f2016-05-19 17:09:41 -07001189 .is_static_object = is_static_object,
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001190 .fixup_init = fixup_init,
1191 .fixup_activate = fixup_activate,
1192 .fixup_destroy = fixup_destroy,
1193 .fixup_free = fixup_free,
1194};
1195
1196static __initdata struct self_test obj = { .static_init = 0 };
1197
1198static void __init debug_objects_selftest(void)
1199{
1200 int fixups, oldfixups, warnings, oldwarnings;
1201 unsigned long flags;
1202
1203 local_irq_save(flags);
1204
1205 fixups = oldfixups = debug_objects_fixups;
1206 warnings = oldwarnings = debug_objects_warnings;
1207 descr_test = &descr_type_test;
1208
1209 debug_object_init(&obj, &descr_type_test);
1210 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1211 goto out;
1212 debug_object_activate(&obj, &descr_type_test);
1213 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1214 goto out;
1215 debug_object_activate(&obj, &descr_type_test);
1216 if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
1217 goto out;
1218 debug_object_deactivate(&obj, &descr_type_test);
1219 if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
1220 goto out;
1221 debug_object_destroy(&obj, &descr_type_test);
1222 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
1223 goto out;
1224 debug_object_init(&obj, &descr_type_test);
1225 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1226 goto out;
1227 debug_object_activate(&obj, &descr_type_test);
1228 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1229 goto out;
1230 debug_object_deactivate(&obj, &descr_type_test);
1231 if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
1232 goto out;
1233 debug_object_free(&obj, &descr_type_test);
1234 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1235 goto out;
1236
1237 obj.static_init = 1;
1238 debug_object_activate(&obj, &descr_type_test);
Stephen Boyd9f78ff02012-03-05 14:59:17 -08001239 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001240 goto out;
1241 debug_object_init(&obj, &descr_type_test);
1242 if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
1243 goto out;
1244 debug_object_free(&obj, &descr_type_test);
1245 if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
1246 goto out;
1247
1248#ifdef CONFIG_DEBUG_OBJECTS_FREE
1249 debug_object_init(&obj, &descr_type_test);
1250 if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
1251 goto out;
1252 debug_object_activate(&obj, &descr_type_test);
1253 if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
1254 goto out;
1255 __debug_check_no_obj_freed(&obj, sizeof(obj));
1256 if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
1257 goto out;
1258#endif
Fabian Frederick719e4842014-06-04 16:06:04 -07001259 pr_info("selftest passed\n");
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001260
1261out:
1262 debug_objects_fixups = oldfixups;
1263 debug_objects_warnings = oldwarnings;
1264 descr_test = NULL;
1265
1266 local_irq_restore(flags);
1267}
1268#else
1269static inline void debug_objects_selftest(void) { }
1270#endif
1271
1272/*
1273 * Called during early boot to initialize the hash buckets and link
1274 * the static object pool objects into the poll list. After this call
1275 * the object tracker is fully operational.
1276 */
1277void __init debug_objects_early_init(void)
1278{
1279 int i;
1280
1281 for (i = 0; i < ODEBUG_HASH_SIZE; i++)
Thomas Gleixneraef9cb02009-11-17 18:11:28 +01001282 raw_spin_lock_init(&obj_hash[i].lock);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001283
1284 for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1285 hlist_add_head(&obj_static_pool[i].node, &obj_pool);
1286}
1287
1288/*
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001289 * Convert the statically allocated objects to dynamic ones:
1290 */
Henrik Kretzschmar1fb2f772010-03-26 20:38:35 +01001291static int __init debug_objects_replace_static_objects(void)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001292{
1293 struct debug_bucket *db = obj_hash;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001294 struct hlist_node *tmp;
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001295 struct debug_obj *obj, *new;
1296 HLIST_HEAD(objects);
1297 int i, cnt = 0;
1298
1299 for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
1300 obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
1301 if (!obj)
1302 goto free;
1303 hlist_add_head(&obj->node, &objects);
1304 }
1305
1306 /*
Qian Caia9ee3a62018-12-28 00:32:32 -08001307 * debug_objects_mem_init() is now called early that only one CPU is up
1308 * and interrupts have been disabled, so it is safe to replace the
1309 * active object references.
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001310 */
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001311
1312 /* Remove the statically allocated objects from the pool */
Sasha Levinb67bfe02013-02-27 17:06:00 -08001313 hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001314 hlist_del(&obj->node);
1315 /* Move the allocated objects to the pool */
1316 hlist_move_list(&objects, &obj_pool);
1317
1318 /* Replace the active object references */
1319 for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
1320 hlist_move_list(&db->list, &objects);
1321
Sasha Levinb67bfe02013-02-27 17:06:00 -08001322 hlist_for_each_entry(obj, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001323 new = hlist_entry(obj_pool.first, typeof(*obj), node);
1324 hlist_del(&new->node);
1325 /* copy object data */
1326 *new = *obj;
1327 hlist_add_head(&new->node, &db->list);
1328 cnt++;
1329 }
1330 }
1331
Fabian Frederickc0f35cc2014-06-04 16:06:05 -07001332 pr_debug("%d of %d active objects replaced\n",
1333 cnt, obj_pool_used);
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001334 return 0;
1335free:
Sasha Levinb67bfe02013-02-27 17:06:00 -08001336 hlist_for_each_entry_safe(obj, tmp, &objects, node) {
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001337 hlist_del(&obj->node);
1338 kmem_cache_free(obj_cache, obj);
1339 }
1340 return -ENOMEM;
1341}
1342
1343/*
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001344 * Called after the kmem_caches are functional to setup a dedicated
1345 * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
1346 * prevents that the debug code is called on kmem_cache_free() for the
1347 * debug tracker objects to avoid recursive calls.
1348 */
1349void __init debug_objects_mem_init(void)
1350{
Waiman Long634d61f2019-05-20 10:14:47 -04001351 int cpu, extras;
Waiman Longd86998b2019-05-20 10:14:46 -04001352
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001353 if (!debug_objects_enabled)
1354 return;
1355
Waiman Longd86998b2019-05-20 10:14:46 -04001356 /*
1357 * Initialize the percpu object pools
1358 *
1359 * Initialization is not strictly necessary, but was done for
1360 * completeness.
1361 */
1362 for_each_possible_cpu(cpu)
1363 INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool.free_objs, cpu));
1364
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001365 obj_cache = kmem_cache_create("debug_objects_cache",
1366 sizeof (struct debug_obj), 0,
Qian Cai8de456c2018-11-30 14:09:48 -08001367 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
1368 NULL);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001369
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001370 if (!obj_cache || debug_objects_replace_static_objects()) {
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001371 debug_objects_enabled = 0;
Zhong Jiang3ff4f802018-08-01 00:24:58 +08001372 kmem_cache_destroy(obj_cache);
Fabian Frederick719e4842014-06-04 16:06:04 -07001373 pr_warn("out of memory.\n");
Thomas Gleixner1be1cb72009-03-16 18:53:18 +01001374 } else
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001375 debug_objects_selftest();
Waiman Long634d61f2019-05-20 10:14:47 -04001376
1377 /*
1378 * Increase the thresholds for allocating and freeing objects
1379 * according to the number of possible CPUs available in the system.
1380 */
1381 extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
1382 debug_objects_pool_size += extras;
1383 debug_objects_pool_min_level += extras;
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07001384}