xref: /linux-6.15/lib/debugobjects.c (revision a7344a68)
13ac7fe5aSThomas Gleixner /*
23ac7fe5aSThomas Gleixner  * Generic infrastructure for lifetime debugging of objects.
33ac7fe5aSThomas Gleixner  *
43ac7fe5aSThomas Gleixner  * Started by Thomas Gleixner
53ac7fe5aSThomas Gleixner  *
63ac7fe5aSThomas Gleixner  * Copyright (C) 2008, Thomas Gleixner <[email protected]>
73ac7fe5aSThomas Gleixner  *
83ac7fe5aSThomas Gleixner  * For licencing details see kernel-base/COPYING
93ac7fe5aSThomas Gleixner  */
10719e4843SFabian Frederick 
11719e4843SFabian Frederick #define pr_fmt(fmt) "ODEBUG: " fmt
12719e4843SFabian Frederick 
133ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
143ac7fe5aSThomas Gleixner #include <linux/interrupt.h>
15d43c36dcSAlexey Dobriyan #include <linux/sched.h>
1668db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
173ac7fe5aSThomas Gleixner #include <linux/seq_file.h>
183ac7fe5aSThomas Gleixner #include <linux/debugfs.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
203ac7fe5aSThomas Gleixner #include <linux/hash.h>
21caba4cbbSWaiman Long #include <linux/kmemleak.h>
223ac7fe5aSThomas Gleixner 
233ac7fe5aSThomas Gleixner #define ODEBUG_HASH_BITS	14
243ac7fe5aSThomas Gleixner #define ODEBUG_HASH_SIZE	(1 << ODEBUG_HASH_BITS)
253ac7fe5aSThomas Gleixner 
260b6ec8c0SChristian Borntraeger #define ODEBUG_POOL_SIZE	1024
273ac7fe5aSThomas Gleixner #define ODEBUG_POOL_MIN_LEVEL	256
28d86998b1SWaiman Long #define ODEBUG_POOL_PERCPU_SIZE	64
29634d61f4SWaiman Long #define ODEBUG_BATCH_SIZE	16
303ac7fe5aSThomas Gleixner 
313ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_SHIFT	PAGE_SHIFT
323ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_SIZE	(1 << ODEBUG_CHUNK_SHIFT)
333ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_MASK	(~(ODEBUG_CHUNK_SIZE - 1))
343ac7fe5aSThomas Gleixner 
35*a7344a68SWaiman Long /*
36*a7344a68SWaiman Long  * We limit the freeing of debug objects via workqueue at a maximum
37*a7344a68SWaiman Long  * frequency of 10Hz and about 1024 objects for each freeing operation.
38*a7344a68SWaiman Long  * So it is freeing at most 10k debug objects per second.
39*a7344a68SWaiman Long  */
40*a7344a68SWaiman Long #define ODEBUG_FREE_WORK_MAX	1024
41*a7344a68SWaiman Long #define ODEBUG_FREE_WORK_DELAY	DIV_ROUND_UP(HZ, 10)
42*a7344a68SWaiman Long 
433ac7fe5aSThomas Gleixner struct debug_bucket {
443ac7fe5aSThomas Gleixner 	struct hlist_head	list;
45aef9cb05SThomas Gleixner 	raw_spinlock_t		lock;
463ac7fe5aSThomas Gleixner };
473ac7fe5aSThomas Gleixner 
48d86998b1SWaiman Long /*
49d86998b1SWaiman Long  * Debug object percpu free list
50d86998b1SWaiman Long  * Access is protected by disabling irq
51d86998b1SWaiman Long  */
52d86998b1SWaiman Long struct debug_percpu_free {
53d86998b1SWaiman Long 	struct hlist_head	free_objs;
54d86998b1SWaiman Long 	int			obj_free;
55d86998b1SWaiman Long };
56d86998b1SWaiman Long 
57d86998b1SWaiman Long static DEFINE_PER_CPU(struct debug_percpu_free, percpu_obj_pool);
58d86998b1SWaiman Long 
593ac7fe5aSThomas Gleixner static struct debug_bucket	obj_hash[ODEBUG_HASH_SIZE];
603ac7fe5aSThomas Gleixner 
611be1cb7bSThomas Gleixner static struct debug_obj		obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
623ac7fe5aSThomas Gleixner 
63aef9cb05SThomas Gleixner static DEFINE_RAW_SPINLOCK(pool_lock);
643ac7fe5aSThomas Gleixner 
653ac7fe5aSThomas Gleixner static HLIST_HEAD(obj_pool);
6636c4ead6SYang Shi static HLIST_HEAD(obj_to_free);
673ac7fe5aSThomas Gleixner 
68d86998b1SWaiman Long /*
69d86998b1SWaiman Long  * Because of the presence of percpu free pools, obj_pool_free will
70d86998b1SWaiman Long  * under-count those in the percpu free pools. Similarly, obj_pool_used
71d86998b1SWaiman Long  * will over-count those in the percpu free pools. Adjustments will be
72d86998b1SWaiman Long  * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
73d86998b1SWaiman Long  * can be off.
74d86998b1SWaiman Long  */
753ac7fe5aSThomas Gleixner static int			obj_pool_min_free = ODEBUG_POOL_SIZE;
763ac7fe5aSThomas Gleixner static int			obj_pool_free = ODEBUG_POOL_SIZE;
773ac7fe5aSThomas Gleixner static int			obj_pool_used;
783ac7fe5aSThomas Gleixner static int			obj_pool_max_used;
79*a7344a68SWaiman Long static bool			obj_freeing;
8036c4ead6SYang Shi /* The number of objs on the global free list */
8136c4ead6SYang Shi static int			obj_nr_tofree;
823ac7fe5aSThomas Gleixner 
833ac7fe5aSThomas Gleixner static int			debug_objects_maxchain __read_mostly;
84163cf842SArnd Bergmann static int __maybe_unused	debug_objects_maxchecked __read_mostly;
853ac7fe5aSThomas Gleixner static int			debug_objects_fixups __read_mostly;
863ac7fe5aSThomas Gleixner static int			debug_objects_warnings __read_mostly;
873ae70205SIngo Molnar static int			debug_objects_enabled __read_mostly
883ae70205SIngo Molnar 				= CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
8997dd552eSWaiman Long static int			debug_objects_pool_size __read_mostly
9097dd552eSWaiman Long 				= ODEBUG_POOL_SIZE;
9197dd552eSWaiman Long static int			debug_objects_pool_min_level __read_mostly
9297dd552eSWaiman Long 				= ODEBUG_POOL_MIN_LEVEL;
933ac7fe5aSThomas Gleixner static struct debug_obj_descr	*descr_test  __read_mostly;
94d86998b1SWaiman Long static struct kmem_cache	*obj_cache __read_mostly;
953ac7fe5aSThomas Gleixner 
96c4b73aabSWaiman Long /*
970cad93c3SWaiman Long  * Track numbers of kmem_cache_alloc()/free() calls done.
98c4b73aabSWaiman Long  */
990cad93c3SWaiman Long static int			debug_objects_allocated;
100c4b73aabSWaiman Long static int			debug_objects_freed;
101c4b73aabSWaiman Long 
102337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work);
103*a7344a68SWaiman Long static DECLARE_DELAYED_WORK(debug_obj_work, free_obj_work);
104337fff8bSThomas Gleixner 
1053ac7fe5aSThomas Gleixner static int __init enable_object_debug(char *str)
1063ac7fe5aSThomas Gleixner {
1073ac7fe5aSThomas Gleixner 	debug_objects_enabled = 1;
1083ac7fe5aSThomas Gleixner 	return 0;
1093ac7fe5aSThomas Gleixner }
1103e8ebb5cSKyle McMartin 
1113e8ebb5cSKyle McMartin static int __init disable_object_debug(char *str)
1123e8ebb5cSKyle McMartin {
1133e8ebb5cSKyle McMartin 	debug_objects_enabled = 0;
1143e8ebb5cSKyle McMartin 	return 0;
1153e8ebb5cSKyle McMartin }
1163e8ebb5cSKyle McMartin 
1173ac7fe5aSThomas Gleixner early_param("debug_objects", enable_object_debug);
1183e8ebb5cSKyle McMartin early_param("no_debug_objects", disable_object_debug);
1193ac7fe5aSThomas Gleixner 
1203ac7fe5aSThomas Gleixner static const char *obj_states[ODEBUG_STATE_MAX] = {
1213ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_NONE]		= "none",
1223ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_INIT]		= "initialized",
1233ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_INACTIVE]		= "inactive",
1243ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_ACTIVE]		= "active",
1253ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_DESTROYED]	= "destroyed",
1263ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_NOTAVAILABLE]	= "not available",
1273ac7fe5aSThomas Gleixner };
1283ac7fe5aSThomas Gleixner 
1291fda107dSThomas Gleixner static void fill_pool(void)
1303ac7fe5aSThomas Gleixner {
1313ac7fe5aSThomas Gleixner 	gfp_t gfp = GFP_ATOMIC | __GFP_NORETRY | __GFP_NOWARN;
132d26bf505SWaiman Long 	struct debug_obj *obj;
13350db04ddSVegard Nossum 	unsigned long flags;
1343ac7fe5aSThomas Gleixner 
13597dd552eSWaiman Long 	if (likely(obj_pool_free >= debug_objects_pool_min_level))
1361fda107dSThomas Gleixner 		return;
1373ac7fe5aSThomas Gleixner 
13836c4ead6SYang Shi 	/*
13936c4ead6SYang Shi 	 * Reuse objs from the global free list; they will be reinitialized
14036c4ead6SYang Shi 	 * when allocating.
14136c4ead6SYang Shi 	 */
14236c4ead6SYang Shi 	while (obj_nr_tofree && (obj_pool_free < obj_pool_min_free)) {
14336c4ead6SYang Shi 		raw_spin_lock_irqsave(&pool_lock, flags);
14436c4ead6SYang Shi 		/*
14536c4ead6SYang Shi 		 * Recheck with the lock held as the worker thread might have
14636c4ead6SYang Shi 		 * won the race and freed the global free list already.
14736c4ead6SYang Shi 		 */
148d26bf505SWaiman Long 		while (obj_nr_tofree && (obj_pool_free < obj_pool_min_free)) {
14936c4ead6SYang Shi 			obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
15036c4ead6SYang Shi 			hlist_del(&obj->node);
15136c4ead6SYang Shi 			obj_nr_tofree--;
15236c4ead6SYang Shi 			hlist_add_head(&obj->node, &obj_pool);
15336c4ead6SYang Shi 			obj_pool_free++;
15436c4ead6SYang Shi 		}
15536c4ead6SYang Shi 		raw_spin_unlock_irqrestore(&pool_lock, flags);
15636c4ead6SYang Shi 	}
15736c4ead6SYang Shi 
1583ac7fe5aSThomas Gleixner 	if (unlikely(!obj_cache))
1591fda107dSThomas Gleixner 		return;
1603ac7fe5aSThomas Gleixner 
16197dd552eSWaiman Long 	while (obj_pool_free < debug_objects_pool_min_level) {
162d26bf505SWaiman Long 		struct debug_obj *new[ODEBUG_BATCH_SIZE];
163d26bf505SWaiman Long 		int cnt;
1643ac7fe5aSThomas Gleixner 
165d26bf505SWaiman Long 		for (cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) {
166d26bf505SWaiman Long 			new[cnt] = kmem_cache_zalloc(obj_cache, gfp);
167d26bf505SWaiman Long 			if (!new[cnt])
168d26bf505SWaiman Long 				break;
169d26bf505SWaiman Long 		}
170d26bf505SWaiman Long 		if (!cnt)
1713340808cSDan Carpenter 			return;
1723ac7fe5aSThomas Gleixner 
173aef9cb05SThomas Gleixner 		raw_spin_lock_irqsave(&pool_lock, flags);
174d26bf505SWaiman Long 		while (cnt) {
175d26bf505SWaiman Long 			hlist_add_head(&new[--cnt]->node, &obj_pool);
1760cad93c3SWaiman Long 			debug_objects_allocated++;
1773ac7fe5aSThomas Gleixner 			obj_pool_free++;
178d26bf505SWaiman Long 		}
179aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&pool_lock, flags);
1803ac7fe5aSThomas Gleixner 	}
1813ac7fe5aSThomas Gleixner }
1823ac7fe5aSThomas Gleixner 
1833ac7fe5aSThomas Gleixner /*
1843ac7fe5aSThomas Gleixner  * Lookup an object in the hash bucket.
1853ac7fe5aSThomas Gleixner  */
1863ac7fe5aSThomas Gleixner static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
1873ac7fe5aSThomas Gleixner {
1883ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
1893ac7fe5aSThomas Gleixner 	int cnt = 0;
1903ac7fe5aSThomas Gleixner 
191b67bfe0dSSasha Levin 	hlist_for_each_entry(obj, &b->list, node) {
1923ac7fe5aSThomas Gleixner 		cnt++;
1933ac7fe5aSThomas Gleixner 		if (obj->object == addr)
1943ac7fe5aSThomas Gleixner 			return obj;
1953ac7fe5aSThomas Gleixner 	}
1963ac7fe5aSThomas Gleixner 	if (cnt > debug_objects_maxchain)
1973ac7fe5aSThomas Gleixner 		debug_objects_maxchain = cnt;
1983ac7fe5aSThomas Gleixner 
1993ac7fe5aSThomas Gleixner 	return NULL;
2003ac7fe5aSThomas Gleixner }
2013ac7fe5aSThomas Gleixner 
2023ac7fe5aSThomas Gleixner /*
203d86998b1SWaiman Long  * Allocate a new object from the hlist
204d86998b1SWaiman Long  */
205d86998b1SWaiman Long static struct debug_obj *__alloc_object(struct hlist_head *list)
206d86998b1SWaiman Long {
207d86998b1SWaiman Long 	struct debug_obj *obj = NULL;
208d86998b1SWaiman Long 
209d86998b1SWaiman Long 	if (list->first) {
210d86998b1SWaiman Long 		obj = hlist_entry(list->first, typeof(*obj), node);
211d86998b1SWaiman Long 		hlist_del(&obj->node);
212d86998b1SWaiman Long 	}
213d86998b1SWaiman Long 
214d86998b1SWaiman Long 	return obj;
215d86998b1SWaiman Long }
216d86998b1SWaiman Long 
217d86998b1SWaiman Long /*
21850db04ddSVegard Nossum  * Allocate a new object. If the pool is empty, switch off the debugger.
219673d62ccSVegard Nossum  * Must be called with interrupts disabled.
2203ac7fe5aSThomas Gleixner  */
2213ac7fe5aSThomas Gleixner static struct debug_obj *
2223ac7fe5aSThomas Gleixner alloc_object(void *addr, struct debug_bucket *b, struct debug_obj_descr *descr)
2233ac7fe5aSThomas Gleixner {
224634d61f4SWaiman Long 	struct debug_percpu_free *percpu_pool = this_cpu_ptr(&percpu_obj_pool);
225d86998b1SWaiman Long 	struct debug_obj *obj;
226d86998b1SWaiman Long 
227d86998b1SWaiman Long 	if (likely(obj_cache)) {
228d86998b1SWaiman Long 		obj = __alloc_object(&percpu_pool->free_objs);
229d86998b1SWaiman Long 		if (obj) {
230d86998b1SWaiman Long 			percpu_pool->obj_free--;
231d86998b1SWaiman Long 			goto init_obj;
232d86998b1SWaiman Long 		}
233d86998b1SWaiman Long 	}
2343ac7fe5aSThomas Gleixner 
235aef9cb05SThomas Gleixner 	raw_spin_lock(&pool_lock);
236d86998b1SWaiman Long 	obj = __alloc_object(&obj_pool);
237d86998b1SWaiman Long 	if (obj) {
2383ac7fe5aSThomas Gleixner 		obj_pool_used++;
239634d61f4SWaiman Long 		obj_pool_free--;
240634d61f4SWaiman Long 
241634d61f4SWaiman Long 		/*
242634d61f4SWaiman Long 		 * Looking ahead, allocate one batch of debug objects and
243634d61f4SWaiman Long 		 * put them into the percpu free pool.
244634d61f4SWaiman Long 		 */
245634d61f4SWaiman Long 		if (likely(obj_cache)) {
246634d61f4SWaiman Long 			int i;
247634d61f4SWaiman Long 
248634d61f4SWaiman Long 			for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
249634d61f4SWaiman Long 				struct debug_obj *obj2;
250634d61f4SWaiman Long 
251634d61f4SWaiman Long 				obj2 = __alloc_object(&obj_pool);
252634d61f4SWaiman Long 				if (!obj2)
253634d61f4SWaiman Long 					break;
254634d61f4SWaiman Long 				hlist_add_head(&obj2->node,
255634d61f4SWaiman Long 					       &percpu_pool->free_objs);
256634d61f4SWaiman Long 				percpu_pool->obj_free++;
257634d61f4SWaiman Long 				obj_pool_used++;
258634d61f4SWaiman Long 				obj_pool_free--;
259634d61f4SWaiman Long 			}
260634d61f4SWaiman Long 		}
261634d61f4SWaiman Long 
2623ac7fe5aSThomas Gleixner 		if (obj_pool_used > obj_pool_max_used)
2633ac7fe5aSThomas Gleixner 			obj_pool_max_used = obj_pool_used;
2643ac7fe5aSThomas Gleixner 
2653ac7fe5aSThomas Gleixner 		if (obj_pool_free < obj_pool_min_free)
2663ac7fe5aSThomas Gleixner 			obj_pool_min_free = obj_pool_free;
2673ac7fe5aSThomas Gleixner 	}
268aef9cb05SThomas Gleixner 	raw_spin_unlock(&pool_lock);
2693ac7fe5aSThomas Gleixner 
270d86998b1SWaiman Long init_obj:
271d86998b1SWaiman Long 	if (obj) {
272d86998b1SWaiman Long 		obj->object = addr;
273d86998b1SWaiman Long 		obj->descr  = descr;
274d86998b1SWaiman Long 		obj->state  = ODEBUG_STATE_NONE;
275d86998b1SWaiman Long 		obj->astate = 0;
276d86998b1SWaiman Long 		hlist_add_head(&obj->node, &b->list);
277d86998b1SWaiman Long 	}
2783ac7fe5aSThomas Gleixner 	return obj;
2793ac7fe5aSThomas Gleixner }
2803ac7fe5aSThomas Gleixner 
2813ac7fe5aSThomas Gleixner /*
282337fff8bSThomas Gleixner  * workqueue function to free objects.
283858274b6SWaiman Long  *
284858274b6SWaiman Long  * To reduce contention on the global pool_lock, the actual freeing of
285636e1970SYang Shi  * debug objects will be delayed if the pool_lock is busy.
286337fff8bSThomas Gleixner  */
287337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work)
288337fff8bSThomas Gleixner {
28936c4ead6SYang Shi 	struct hlist_node *tmp;
29036c4ead6SYang Shi 	struct debug_obj *obj;
291337fff8bSThomas Gleixner 	unsigned long flags;
29236c4ead6SYang Shi 	HLIST_HEAD(tofree);
293337fff8bSThomas Gleixner 
294*a7344a68SWaiman Long 	WRITE_ONCE(obj_freeing, false);
295858274b6SWaiman Long 	if (!raw_spin_trylock_irqsave(&pool_lock, flags))
296858274b6SWaiman Long 		return;
29736c4ead6SYang Shi 
298*a7344a68SWaiman Long 	if (obj_pool_free >= debug_objects_pool_size)
299*a7344a68SWaiman Long 		goto free_objs;
300*a7344a68SWaiman Long 
30136c4ead6SYang Shi 	/*
30236c4ead6SYang Shi 	 * The objs on the pool list might be allocated before the work is
30336c4ead6SYang Shi 	 * run, so recheck if pool list it full or not, if not fill pool
304*a7344a68SWaiman Long 	 * list from the global free list. As it is likely that a workload
305*a7344a68SWaiman Long 	 * may be gearing up to use more and more objects, don't free any
306*a7344a68SWaiman Long 	 * of them until the next round.
30736c4ead6SYang Shi 	 */
30836c4ead6SYang Shi 	while (obj_nr_tofree && obj_pool_free < debug_objects_pool_size) {
30936c4ead6SYang Shi 		obj = hlist_entry(obj_to_free.first, typeof(*obj), node);
31036c4ead6SYang Shi 		hlist_del(&obj->node);
31136c4ead6SYang Shi 		hlist_add_head(&obj->node, &obj_pool);
31236c4ead6SYang Shi 		obj_pool_free++;
31336c4ead6SYang Shi 		obj_nr_tofree--;
31436c4ead6SYang Shi 	}
315*a7344a68SWaiman Long 	raw_spin_unlock_irqrestore(&pool_lock, flags);
316*a7344a68SWaiman Long 	return;
31736c4ead6SYang Shi 
318*a7344a68SWaiman Long free_objs:
31936c4ead6SYang Shi 	/*
32036c4ead6SYang Shi 	 * Pool list is already full and there are still objs on the free
32136c4ead6SYang Shi 	 * list. Move remaining free objs to a temporary list to free the
32236c4ead6SYang Shi 	 * memory outside the pool_lock held region.
32336c4ead6SYang Shi 	 */
32436c4ead6SYang Shi 	if (obj_nr_tofree) {
32536c4ead6SYang Shi 		hlist_move_list(&obj_to_free, &tofree);
32604148187SArnd Bergmann 		debug_objects_freed += obj_nr_tofree;
32736c4ead6SYang Shi 		obj_nr_tofree = 0;
32836c4ead6SYang Shi 	}
329aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&pool_lock, flags);
33036c4ead6SYang Shi 
33136c4ead6SYang Shi 	hlist_for_each_entry_safe(obj, tmp, &tofree, node) {
33236c4ead6SYang Shi 		hlist_del(&obj->node);
33336c4ead6SYang Shi 		kmem_cache_free(obj_cache, obj);
33436c4ead6SYang Shi 	}
335337fff8bSThomas Gleixner }
336337fff8bSThomas Gleixner 
337*a7344a68SWaiman Long static void __free_object(struct debug_obj *obj)
338636e1970SYang Shi {
339634d61f4SWaiman Long 	struct debug_obj *objs[ODEBUG_BATCH_SIZE];
340634d61f4SWaiman Long 	struct debug_percpu_free *percpu_pool;
341634d61f4SWaiman Long 	int lookahead_count = 0;
342636e1970SYang Shi 	unsigned long flags;
343636e1970SYang Shi 	bool work;
344636e1970SYang Shi 
345d86998b1SWaiman Long 	local_irq_save(flags);
346634d61f4SWaiman Long 	if (!obj_cache)
347634d61f4SWaiman Long 		goto free_to_obj_pool;
348634d61f4SWaiman Long 
349d86998b1SWaiman Long 	/*
350d86998b1SWaiman Long 	 * Try to free it into the percpu pool first.
351d86998b1SWaiman Long 	 */
352d86998b1SWaiman Long 	percpu_pool = this_cpu_ptr(&percpu_obj_pool);
353634d61f4SWaiman Long 	if (percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) {
354d86998b1SWaiman Long 		hlist_add_head(&obj->node, &percpu_pool->free_objs);
355d86998b1SWaiman Long 		percpu_pool->obj_free++;
356d86998b1SWaiman Long 		local_irq_restore(flags);
357*a7344a68SWaiman Long 		return;
358d86998b1SWaiman Long 	}
359d86998b1SWaiman Long 
360634d61f4SWaiman Long 	/*
361634d61f4SWaiman Long 	 * As the percpu pool is full, look ahead and pull out a batch
362634d61f4SWaiman Long 	 * of objects from the percpu pool and free them as well.
363634d61f4SWaiman Long 	 */
364634d61f4SWaiman Long 	for (; lookahead_count < ODEBUG_BATCH_SIZE; lookahead_count++) {
365634d61f4SWaiman Long 		objs[lookahead_count] = __alloc_object(&percpu_pool->free_objs);
366634d61f4SWaiman Long 		if (!objs[lookahead_count])
367634d61f4SWaiman Long 			break;
368634d61f4SWaiman Long 		percpu_pool->obj_free--;
369634d61f4SWaiman Long 	}
370634d61f4SWaiman Long 
371634d61f4SWaiman Long free_to_obj_pool:
372d86998b1SWaiman Long 	raw_spin_lock(&pool_lock);
373*a7344a68SWaiman Long 	work = (obj_pool_free > debug_objects_pool_size) && obj_cache &&
374*a7344a68SWaiman Long 	       (obj_nr_tofree < ODEBUG_FREE_WORK_MAX);
375636e1970SYang Shi 	obj_pool_used--;
376636e1970SYang Shi 
377636e1970SYang Shi 	if (work) {
378636e1970SYang Shi 		obj_nr_tofree++;
379636e1970SYang Shi 		hlist_add_head(&obj->node, &obj_to_free);
380634d61f4SWaiman Long 		if (lookahead_count) {
381634d61f4SWaiman Long 			obj_nr_tofree += lookahead_count;
382634d61f4SWaiman Long 			obj_pool_used -= lookahead_count;
383634d61f4SWaiman Long 			while (lookahead_count) {
384634d61f4SWaiman Long 				hlist_add_head(&objs[--lookahead_count]->node,
385634d61f4SWaiman Long 					       &obj_to_free);
386634d61f4SWaiman Long 			}
387634d61f4SWaiman Long 		}
388*a7344a68SWaiman Long 
389*a7344a68SWaiman Long 		if ((obj_pool_free > debug_objects_pool_size) &&
390*a7344a68SWaiman Long 		    (obj_nr_tofree < ODEBUG_FREE_WORK_MAX)) {
391*a7344a68SWaiman Long 			int i;
392*a7344a68SWaiman Long 
393*a7344a68SWaiman Long 			/*
394*a7344a68SWaiman Long 			 * Free one more batch of objects from obj_pool.
395*a7344a68SWaiman Long 			 */
396*a7344a68SWaiman Long 			for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
397*a7344a68SWaiman Long 				obj = __alloc_object(&obj_pool);
398*a7344a68SWaiman Long 				hlist_add_head(&obj->node, &obj_to_free);
399*a7344a68SWaiman Long 				obj_pool_free--;
400*a7344a68SWaiman Long 				obj_nr_tofree++;
401*a7344a68SWaiman Long 			}
402*a7344a68SWaiman Long 		}
403636e1970SYang Shi 	} else {
404636e1970SYang Shi 		obj_pool_free++;
405636e1970SYang Shi 		hlist_add_head(&obj->node, &obj_pool);
406634d61f4SWaiman Long 		if (lookahead_count) {
407634d61f4SWaiman Long 			obj_pool_free += lookahead_count;
408634d61f4SWaiman Long 			obj_pool_used -= lookahead_count;
409634d61f4SWaiman Long 			while (lookahead_count) {
410634d61f4SWaiman Long 				hlist_add_head(&objs[--lookahead_count]->node,
411634d61f4SWaiman Long 					       &obj_pool);
412634d61f4SWaiman Long 			}
413634d61f4SWaiman Long 		}
414636e1970SYang Shi 	}
415d86998b1SWaiman Long 	raw_spin_unlock(&pool_lock);
416d86998b1SWaiman Long 	local_irq_restore(flags);
417636e1970SYang Shi }
418636e1970SYang Shi 
419337fff8bSThomas Gleixner /*
420337fff8bSThomas Gleixner  * Put the object back into the pool and schedule work to free objects
421337fff8bSThomas Gleixner  * if necessary.
4223ac7fe5aSThomas Gleixner  */
4233ac7fe5aSThomas Gleixner static void free_object(struct debug_obj *obj)
4243ac7fe5aSThomas Gleixner {
425*a7344a68SWaiman Long 	__free_object(obj);
426*a7344a68SWaiman Long 	if (!obj_freeing && obj_nr_tofree) {
427*a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
428*a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
429*a7344a68SWaiman Long 	}
4303ac7fe5aSThomas Gleixner }
4313ac7fe5aSThomas Gleixner 
4323ac7fe5aSThomas Gleixner /*
4333ac7fe5aSThomas Gleixner  * We run out of memory. That means we probably have tons of objects
4343ac7fe5aSThomas Gleixner  * allocated.
4353ac7fe5aSThomas Gleixner  */
4363ac7fe5aSThomas Gleixner static void debug_objects_oom(void)
4373ac7fe5aSThomas Gleixner {
4383ac7fe5aSThomas Gleixner 	struct debug_bucket *db = obj_hash;
439b67bfe0dSSasha Levin 	struct hlist_node *tmp;
440673d62ccSVegard Nossum 	HLIST_HEAD(freelist);
4413ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
4423ac7fe5aSThomas Gleixner 	unsigned long flags;
4433ac7fe5aSThomas Gleixner 	int i;
4443ac7fe5aSThomas Gleixner 
445719e4843SFabian Frederick 	pr_warn("Out of memory. ODEBUG disabled\n");
4463ac7fe5aSThomas Gleixner 
4473ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
448aef9cb05SThomas Gleixner 		raw_spin_lock_irqsave(&db->lock, flags);
449673d62ccSVegard Nossum 		hlist_move_list(&db->list, &freelist);
450aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
451673d62ccSVegard Nossum 
452673d62ccSVegard Nossum 		/* Now free them */
453b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(obj, tmp, &freelist, node) {
4543ac7fe5aSThomas Gleixner 			hlist_del(&obj->node);
4553ac7fe5aSThomas Gleixner 			free_object(obj);
4563ac7fe5aSThomas Gleixner 		}
4573ac7fe5aSThomas Gleixner 	}
4583ac7fe5aSThomas Gleixner }
4593ac7fe5aSThomas Gleixner 
4603ac7fe5aSThomas Gleixner /*
4613ac7fe5aSThomas Gleixner  * We use the pfn of the address for the hash. That way we can check
4623ac7fe5aSThomas Gleixner  * for freed objects simply by checking the affected bucket.
4633ac7fe5aSThomas Gleixner  */
4643ac7fe5aSThomas Gleixner static struct debug_bucket *get_bucket(unsigned long addr)
4653ac7fe5aSThomas Gleixner {
4663ac7fe5aSThomas Gleixner 	unsigned long hash;
4673ac7fe5aSThomas Gleixner 
4683ac7fe5aSThomas Gleixner 	hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
4693ac7fe5aSThomas Gleixner 	return &obj_hash[hash];
4703ac7fe5aSThomas Gleixner }
4713ac7fe5aSThomas Gleixner 
4723ac7fe5aSThomas Gleixner static void debug_print_object(struct debug_obj *obj, char *msg)
4733ac7fe5aSThomas Gleixner {
47499777288SStanislaw Gruszka 	struct debug_obj_descr *descr = obj->descr;
4753ac7fe5aSThomas Gleixner 	static int limit;
4763ac7fe5aSThomas Gleixner 
47799777288SStanislaw Gruszka 	if (limit < 5 && descr != descr_test) {
47899777288SStanislaw Gruszka 		void *hint = descr->debug_hint ?
47999777288SStanislaw Gruszka 			descr->debug_hint(obj->object) : NULL;
4803ac7fe5aSThomas Gleixner 		limit++;
481a5d8e467SMathieu Desnoyers 		WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
48299777288SStanislaw Gruszka 				 "object type: %s hint: %pS\n",
483a5d8e467SMathieu Desnoyers 			msg, obj_states[obj->state], obj->astate,
48499777288SStanislaw Gruszka 			descr->name, hint);
4853ac7fe5aSThomas Gleixner 	}
4863ac7fe5aSThomas Gleixner 	debug_objects_warnings++;
4873ac7fe5aSThomas Gleixner }
4883ac7fe5aSThomas Gleixner 
4893ac7fe5aSThomas Gleixner /*
4903ac7fe5aSThomas Gleixner  * Try to repair the damage, so we have a better chance to get useful
4913ac7fe5aSThomas Gleixner  * debug output.
4923ac7fe5aSThomas Gleixner  */
493b1e4d9d8SDu, Changbin static bool
494b1e4d9d8SDu, Changbin debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
4953ac7fe5aSThomas Gleixner 		   void * addr, enum debug_obj_state state)
4963ac7fe5aSThomas Gleixner {
497b1e4d9d8SDu, Changbin 	if (fixup && fixup(addr, state)) {
498b1e4d9d8SDu, Changbin 		debug_objects_fixups++;
499b1e4d9d8SDu, Changbin 		return true;
500b1e4d9d8SDu, Changbin 	}
501b1e4d9d8SDu, Changbin 	return false;
5023ac7fe5aSThomas Gleixner }
5033ac7fe5aSThomas Gleixner 
5043ac7fe5aSThomas Gleixner static void debug_object_is_on_stack(void *addr, int onstack)
5053ac7fe5aSThomas Gleixner {
5063ac7fe5aSThomas Gleixner 	int is_on_stack;
5073ac7fe5aSThomas Gleixner 	static int limit;
5083ac7fe5aSThomas Gleixner 
5093ac7fe5aSThomas Gleixner 	if (limit > 4)
5103ac7fe5aSThomas Gleixner 		return;
5113ac7fe5aSThomas Gleixner 
5128b05c7e6SFUJITA Tomonori 	is_on_stack = object_is_on_stack(addr);
5133ac7fe5aSThomas Gleixner 	if (is_on_stack == onstack)
5143ac7fe5aSThomas Gleixner 		return;
5153ac7fe5aSThomas Gleixner 
5163ac7fe5aSThomas Gleixner 	limit++;
5173ac7fe5aSThomas Gleixner 	if (is_on_stack)
518fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
519fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
5203ac7fe5aSThomas Gleixner 	else
521fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
522fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
523fc91a3c4SJoel Fernandes (Google) 
5243ac7fe5aSThomas Gleixner 	WARN_ON(1);
5253ac7fe5aSThomas Gleixner }
5263ac7fe5aSThomas Gleixner 
5273ac7fe5aSThomas Gleixner static void
5283ac7fe5aSThomas Gleixner __debug_object_init(void *addr, struct debug_obj_descr *descr, int onstack)
5293ac7fe5aSThomas Gleixner {
5303ac7fe5aSThomas Gleixner 	enum debug_obj_state state;
5313ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
5323ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
5333ac7fe5aSThomas Gleixner 	unsigned long flags;
5343ac7fe5aSThomas Gleixner 
53550db04ddSVegard Nossum 	fill_pool();
53650db04ddSVegard Nossum 
5373ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
5383ac7fe5aSThomas Gleixner 
539aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
5403ac7fe5aSThomas Gleixner 
5413ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
5423ac7fe5aSThomas Gleixner 	if (!obj) {
5433ac7fe5aSThomas Gleixner 		obj = alloc_object(addr, db, descr);
5443ac7fe5aSThomas Gleixner 		if (!obj) {
5453ac7fe5aSThomas Gleixner 			debug_objects_enabled = 0;
546aef9cb05SThomas Gleixner 			raw_spin_unlock_irqrestore(&db->lock, flags);
5473ac7fe5aSThomas Gleixner 			debug_objects_oom();
5483ac7fe5aSThomas Gleixner 			return;
5493ac7fe5aSThomas Gleixner 		}
5503ac7fe5aSThomas Gleixner 		debug_object_is_on_stack(addr, onstack);
5513ac7fe5aSThomas Gleixner 	}
5523ac7fe5aSThomas Gleixner 
5533ac7fe5aSThomas Gleixner 	switch (obj->state) {
5543ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
5553ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
5563ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
5573ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_INIT;
5583ac7fe5aSThomas Gleixner 		break;
5593ac7fe5aSThomas Gleixner 
5603ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
5613ac7fe5aSThomas Gleixner 		debug_print_object(obj, "init");
5623ac7fe5aSThomas Gleixner 		state = obj->state;
563aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
5643ac7fe5aSThomas Gleixner 		debug_object_fixup(descr->fixup_init, addr, state);
5653ac7fe5aSThomas Gleixner 		return;
5663ac7fe5aSThomas Gleixner 
5673ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_DESTROYED:
5683ac7fe5aSThomas Gleixner 		debug_print_object(obj, "init");
5693ac7fe5aSThomas Gleixner 		break;
5703ac7fe5aSThomas Gleixner 	default:
5713ac7fe5aSThomas Gleixner 		break;
5723ac7fe5aSThomas Gleixner 	}
5733ac7fe5aSThomas Gleixner 
574aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
5753ac7fe5aSThomas Gleixner }
5763ac7fe5aSThomas Gleixner 
5773ac7fe5aSThomas Gleixner /**
5783ac7fe5aSThomas Gleixner  * debug_object_init - debug checks when an object is initialized
5793ac7fe5aSThomas Gleixner  * @addr:	address of the object
5803ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
5813ac7fe5aSThomas Gleixner  */
5823ac7fe5aSThomas Gleixner void debug_object_init(void *addr, struct debug_obj_descr *descr)
5833ac7fe5aSThomas Gleixner {
5843ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
5853ac7fe5aSThomas Gleixner 		return;
5863ac7fe5aSThomas Gleixner 
5873ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 0);
5883ac7fe5aSThomas Gleixner }
589f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init);
5903ac7fe5aSThomas Gleixner 
5913ac7fe5aSThomas Gleixner /**
5923ac7fe5aSThomas Gleixner  * debug_object_init_on_stack - debug checks when an object on stack is
5933ac7fe5aSThomas Gleixner  *				initialized
5943ac7fe5aSThomas Gleixner  * @addr:	address of the object
5953ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
5963ac7fe5aSThomas Gleixner  */
5973ac7fe5aSThomas Gleixner void debug_object_init_on_stack(void *addr, struct debug_obj_descr *descr)
5983ac7fe5aSThomas Gleixner {
5993ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
6003ac7fe5aSThomas Gleixner 		return;
6013ac7fe5aSThomas Gleixner 
6023ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 1);
6033ac7fe5aSThomas Gleixner }
604f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
6053ac7fe5aSThomas Gleixner 
6063ac7fe5aSThomas Gleixner /**
6073ac7fe5aSThomas Gleixner  * debug_object_activate - debug checks when an object is activated
6083ac7fe5aSThomas Gleixner  * @addr:	address of the object
6093ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
610b778ae25SPaul E. McKenney  * Returns 0 for success, -EINVAL for check failed.
6113ac7fe5aSThomas Gleixner  */
612b778ae25SPaul E. McKenney int debug_object_activate(void *addr, struct debug_obj_descr *descr)
6133ac7fe5aSThomas Gleixner {
6143ac7fe5aSThomas Gleixner 	enum debug_obj_state state;
6153ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
6163ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
6173ac7fe5aSThomas Gleixner 	unsigned long flags;
618b778ae25SPaul E. McKenney 	int ret;
619feac18ddSStephen Boyd 	struct debug_obj o = { .object = addr,
620feac18ddSStephen Boyd 			       .state = ODEBUG_STATE_NOTAVAILABLE,
621feac18ddSStephen Boyd 			       .descr = descr };
6223ac7fe5aSThomas Gleixner 
6233ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
624b778ae25SPaul E. McKenney 		return 0;
6253ac7fe5aSThomas Gleixner 
6263ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
6273ac7fe5aSThomas Gleixner 
628aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
6293ac7fe5aSThomas Gleixner 
6303ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
6313ac7fe5aSThomas Gleixner 	if (obj) {
6323ac7fe5aSThomas Gleixner 		switch (obj->state) {
6333ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
6343ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
6353ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_ACTIVE;
636b778ae25SPaul E. McKenney 			ret = 0;
6373ac7fe5aSThomas Gleixner 			break;
6383ac7fe5aSThomas Gleixner 
6393ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_ACTIVE:
6403ac7fe5aSThomas Gleixner 			debug_print_object(obj, "activate");
6413ac7fe5aSThomas Gleixner 			state = obj->state;
642aef9cb05SThomas Gleixner 			raw_spin_unlock_irqrestore(&db->lock, flags);
643b778ae25SPaul E. McKenney 			ret = debug_object_fixup(descr->fixup_activate, addr, state);
644e7a8e78bSDu, Changbin 			return ret ? 0 : -EINVAL;
6453ac7fe5aSThomas Gleixner 
6463ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_DESTROYED:
6473ac7fe5aSThomas Gleixner 			debug_print_object(obj, "activate");
648b778ae25SPaul E. McKenney 			ret = -EINVAL;
6493ac7fe5aSThomas Gleixner 			break;
6503ac7fe5aSThomas Gleixner 		default:
651b778ae25SPaul E. McKenney 			ret = 0;
6523ac7fe5aSThomas Gleixner 			break;
6533ac7fe5aSThomas Gleixner 		}
654aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
655b778ae25SPaul E. McKenney 		return ret;
6563ac7fe5aSThomas Gleixner 	}
6573ac7fe5aSThomas Gleixner 
658aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
6593ac7fe5aSThomas Gleixner 	/*
660b9fdac7fSDu, Changbin 	 * We are here when a static object is activated. We
661b9fdac7fSDu, Changbin 	 * let the type specific code confirm whether this is
662b9fdac7fSDu, Changbin 	 * true or not. if true, we just make sure that the
663b9fdac7fSDu, Changbin 	 * static object is tracked in the object tracker. If
664b9fdac7fSDu, Changbin 	 * not, this must be a bug, so we try to fix it up.
6653ac7fe5aSThomas Gleixner 	 */
666b9fdac7fSDu, Changbin 	if (descr->is_static_object && descr->is_static_object(addr)) {
667b9fdac7fSDu, Changbin 		/* track this static object */
668b9fdac7fSDu, Changbin 		debug_object_init(addr, descr);
669b9fdac7fSDu, Changbin 		debug_object_activate(addr, descr);
670b9fdac7fSDu, Changbin 	} else {
671feac18ddSStephen Boyd 		debug_print_object(&o, "activate");
672b9fdac7fSDu, Changbin 		ret = debug_object_fixup(descr->fixup_activate, addr,
673b9fdac7fSDu, Changbin 					ODEBUG_STATE_NOTAVAILABLE);
674b9fdac7fSDu, Changbin 		return ret ? 0 : -EINVAL;
675b778ae25SPaul E. McKenney 	}
676b778ae25SPaul E. McKenney 	return 0;
6773ac7fe5aSThomas Gleixner }
678f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_activate);
6793ac7fe5aSThomas Gleixner 
6803ac7fe5aSThomas Gleixner /**
6813ac7fe5aSThomas Gleixner  * debug_object_deactivate - debug checks when an object is deactivated
6823ac7fe5aSThomas Gleixner  * @addr:	address of the object
6833ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
6843ac7fe5aSThomas Gleixner  */
6853ac7fe5aSThomas Gleixner void debug_object_deactivate(void *addr, struct debug_obj_descr *descr)
6863ac7fe5aSThomas Gleixner {
6873ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
6883ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
6893ac7fe5aSThomas Gleixner 	unsigned long flags;
6903ac7fe5aSThomas Gleixner 
6913ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
6923ac7fe5aSThomas Gleixner 		return;
6933ac7fe5aSThomas Gleixner 
6943ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
6953ac7fe5aSThomas Gleixner 
696aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
6973ac7fe5aSThomas Gleixner 
6983ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
6993ac7fe5aSThomas Gleixner 	if (obj) {
7003ac7fe5aSThomas Gleixner 		switch (obj->state) {
7013ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
7023ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
7033ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_ACTIVE:
704a5d8e467SMathieu Desnoyers 			if (!obj->astate)
7053ac7fe5aSThomas Gleixner 				obj->state = ODEBUG_STATE_INACTIVE;
706a5d8e467SMathieu Desnoyers 			else
707a5d8e467SMathieu Desnoyers 				debug_print_object(obj, "deactivate");
7083ac7fe5aSThomas Gleixner 			break;
7093ac7fe5aSThomas Gleixner 
7103ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_DESTROYED:
7113ac7fe5aSThomas Gleixner 			debug_print_object(obj, "deactivate");
7123ac7fe5aSThomas Gleixner 			break;
7133ac7fe5aSThomas Gleixner 		default:
7143ac7fe5aSThomas Gleixner 			break;
7153ac7fe5aSThomas Gleixner 		}
7163ac7fe5aSThomas Gleixner 	} else {
7173ac7fe5aSThomas Gleixner 		struct debug_obj o = { .object = addr,
7183ac7fe5aSThomas Gleixner 				       .state = ODEBUG_STATE_NOTAVAILABLE,
7193ac7fe5aSThomas Gleixner 				       .descr = descr };
7203ac7fe5aSThomas Gleixner 
7213ac7fe5aSThomas Gleixner 		debug_print_object(&o, "deactivate");
7223ac7fe5aSThomas Gleixner 	}
7233ac7fe5aSThomas Gleixner 
724aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
7253ac7fe5aSThomas Gleixner }
726f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_deactivate);
7273ac7fe5aSThomas Gleixner 
7283ac7fe5aSThomas Gleixner /**
7293ac7fe5aSThomas Gleixner  * debug_object_destroy - debug checks when an object is destroyed
7303ac7fe5aSThomas Gleixner  * @addr:	address of the object
7313ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7323ac7fe5aSThomas Gleixner  */
7333ac7fe5aSThomas Gleixner void debug_object_destroy(void *addr, struct debug_obj_descr *descr)
7343ac7fe5aSThomas Gleixner {
7353ac7fe5aSThomas Gleixner 	enum debug_obj_state state;
7363ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
7373ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
7383ac7fe5aSThomas Gleixner 	unsigned long flags;
7393ac7fe5aSThomas Gleixner 
7403ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7413ac7fe5aSThomas Gleixner 		return;
7423ac7fe5aSThomas Gleixner 
7433ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
7443ac7fe5aSThomas Gleixner 
745aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
7463ac7fe5aSThomas Gleixner 
7473ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
7483ac7fe5aSThomas Gleixner 	if (!obj)
7493ac7fe5aSThomas Gleixner 		goto out_unlock;
7503ac7fe5aSThomas Gleixner 
7513ac7fe5aSThomas Gleixner 	switch (obj->state) {
7523ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
7533ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
7543ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
7553ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_DESTROYED;
7563ac7fe5aSThomas Gleixner 		break;
7573ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
7583ac7fe5aSThomas Gleixner 		debug_print_object(obj, "destroy");
7593ac7fe5aSThomas Gleixner 		state = obj->state;
760aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
7613ac7fe5aSThomas Gleixner 		debug_object_fixup(descr->fixup_destroy, addr, state);
7623ac7fe5aSThomas Gleixner 		return;
7633ac7fe5aSThomas Gleixner 
7643ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_DESTROYED:
7653ac7fe5aSThomas Gleixner 		debug_print_object(obj, "destroy");
7663ac7fe5aSThomas Gleixner 		break;
7673ac7fe5aSThomas Gleixner 	default:
7683ac7fe5aSThomas Gleixner 		break;
7693ac7fe5aSThomas Gleixner 	}
7703ac7fe5aSThomas Gleixner out_unlock:
771aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
7723ac7fe5aSThomas Gleixner }
773f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_destroy);
7743ac7fe5aSThomas Gleixner 
7753ac7fe5aSThomas Gleixner /**
7763ac7fe5aSThomas Gleixner  * debug_object_free - debug checks when an object is freed
7773ac7fe5aSThomas Gleixner  * @addr:	address of the object
7783ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7793ac7fe5aSThomas Gleixner  */
7803ac7fe5aSThomas Gleixner void debug_object_free(void *addr, struct debug_obj_descr *descr)
7813ac7fe5aSThomas Gleixner {
7823ac7fe5aSThomas Gleixner 	enum debug_obj_state state;
7833ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
7843ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
7853ac7fe5aSThomas Gleixner 	unsigned long flags;
7863ac7fe5aSThomas Gleixner 
7873ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7883ac7fe5aSThomas Gleixner 		return;
7893ac7fe5aSThomas Gleixner 
7903ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
7913ac7fe5aSThomas Gleixner 
792aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
7933ac7fe5aSThomas Gleixner 
7943ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
7953ac7fe5aSThomas Gleixner 	if (!obj)
7963ac7fe5aSThomas Gleixner 		goto out_unlock;
7973ac7fe5aSThomas Gleixner 
7983ac7fe5aSThomas Gleixner 	switch (obj->state) {
7993ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
8003ac7fe5aSThomas Gleixner 		debug_print_object(obj, "free");
8013ac7fe5aSThomas Gleixner 		state = obj->state;
802aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
8033ac7fe5aSThomas Gleixner 		debug_object_fixup(descr->fixup_free, addr, state);
8043ac7fe5aSThomas Gleixner 		return;
8053ac7fe5aSThomas Gleixner 	default:
8063ac7fe5aSThomas Gleixner 		hlist_del(&obj->node);
807aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
8083ac7fe5aSThomas Gleixner 		free_object(obj);
809673d62ccSVegard Nossum 		return;
8103ac7fe5aSThomas Gleixner 	}
8113ac7fe5aSThomas Gleixner out_unlock:
812aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
8133ac7fe5aSThomas Gleixner }
814f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_free);
8153ac7fe5aSThomas Gleixner 
816a5d8e467SMathieu Desnoyers /**
817b84d435cSChristine Chan  * debug_object_assert_init - debug checks when object should be init-ed
818b84d435cSChristine Chan  * @addr:	address of the object
819b84d435cSChristine Chan  * @descr:	pointer to an object specific debug description structure
820b84d435cSChristine Chan  */
821b84d435cSChristine Chan void debug_object_assert_init(void *addr, struct debug_obj_descr *descr)
822b84d435cSChristine Chan {
823b84d435cSChristine Chan 	struct debug_bucket *db;
824b84d435cSChristine Chan 	struct debug_obj *obj;
825b84d435cSChristine Chan 	unsigned long flags;
826b84d435cSChristine Chan 
827b84d435cSChristine Chan 	if (!debug_objects_enabled)
828b84d435cSChristine Chan 		return;
829b84d435cSChristine Chan 
830b84d435cSChristine Chan 	db = get_bucket((unsigned long) addr);
831b84d435cSChristine Chan 
832b84d435cSChristine Chan 	raw_spin_lock_irqsave(&db->lock, flags);
833b84d435cSChristine Chan 
834b84d435cSChristine Chan 	obj = lookup_object(addr, db);
835b84d435cSChristine Chan 	if (!obj) {
836b84d435cSChristine Chan 		struct debug_obj o = { .object = addr,
837b84d435cSChristine Chan 				       .state = ODEBUG_STATE_NOTAVAILABLE,
838b84d435cSChristine Chan 				       .descr = descr };
839b84d435cSChristine Chan 
840b84d435cSChristine Chan 		raw_spin_unlock_irqrestore(&db->lock, flags);
841b84d435cSChristine Chan 		/*
842b9fdac7fSDu, Changbin 		 * Maybe the object is static, and we let the type specific
843b9fdac7fSDu, Changbin 		 * code confirm. Track this static object if true, else invoke
844b9fdac7fSDu, Changbin 		 * fixup.
845b84d435cSChristine Chan 		 */
846b9fdac7fSDu, Changbin 		if (descr->is_static_object && descr->is_static_object(addr)) {
847b9fdac7fSDu, Changbin 			/* Track this static object */
848b9fdac7fSDu, Changbin 			debug_object_init(addr, descr);
849b9fdac7fSDu, Changbin 		} else {
850b84d435cSChristine Chan 			debug_print_object(&o, "assert_init");
851b9fdac7fSDu, Changbin 			debug_object_fixup(descr->fixup_assert_init, addr,
852b9fdac7fSDu, Changbin 					   ODEBUG_STATE_NOTAVAILABLE);
853b9fdac7fSDu, Changbin 		}
854b84d435cSChristine Chan 		return;
855b84d435cSChristine Chan 	}
856b84d435cSChristine Chan 
857b84d435cSChristine Chan 	raw_spin_unlock_irqrestore(&db->lock, flags);
858b84d435cSChristine Chan }
859f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_assert_init);
860b84d435cSChristine Chan 
861b84d435cSChristine Chan /**
862a5d8e467SMathieu Desnoyers  * debug_object_active_state - debug checks object usage state machine
863a5d8e467SMathieu Desnoyers  * @addr:	address of the object
864a5d8e467SMathieu Desnoyers  * @descr:	pointer to an object specific debug description structure
865a5d8e467SMathieu Desnoyers  * @expect:	expected state
866a5d8e467SMathieu Desnoyers  * @next:	state to move to if expected state is found
867a5d8e467SMathieu Desnoyers  */
868a5d8e467SMathieu Desnoyers void
869a5d8e467SMathieu Desnoyers debug_object_active_state(void *addr, struct debug_obj_descr *descr,
870a5d8e467SMathieu Desnoyers 			  unsigned int expect, unsigned int next)
871a5d8e467SMathieu Desnoyers {
872a5d8e467SMathieu Desnoyers 	struct debug_bucket *db;
873a5d8e467SMathieu Desnoyers 	struct debug_obj *obj;
874a5d8e467SMathieu Desnoyers 	unsigned long flags;
875a5d8e467SMathieu Desnoyers 
876a5d8e467SMathieu Desnoyers 	if (!debug_objects_enabled)
877a5d8e467SMathieu Desnoyers 		return;
878a5d8e467SMathieu Desnoyers 
879a5d8e467SMathieu Desnoyers 	db = get_bucket((unsigned long) addr);
880a5d8e467SMathieu Desnoyers 
881a5d8e467SMathieu Desnoyers 	raw_spin_lock_irqsave(&db->lock, flags);
882a5d8e467SMathieu Desnoyers 
883a5d8e467SMathieu Desnoyers 	obj = lookup_object(addr, db);
884a5d8e467SMathieu Desnoyers 	if (obj) {
885a5d8e467SMathieu Desnoyers 		switch (obj->state) {
886a5d8e467SMathieu Desnoyers 		case ODEBUG_STATE_ACTIVE:
887a5d8e467SMathieu Desnoyers 			if (obj->astate == expect)
888a5d8e467SMathieu Desnoyers 				obj->astate = next;
889a5d8e467SMathieu Desnoyers 			else
890a5d8e467SMathieu Desnoyers 				debug_print_object(obj, "active_state");
891a5d8e467SMathieu Desnoyers 			break;
892a5d8e467SMathieu Desnoyers 
893a5d8e467SMathieu Desnoyers 		default:
894a5d8e467SMathieu Desnoyers 			debug_print_object(obj, "active_state");
895a5d8e467SMathieu Desnoyers 			break;
896a5d8e467SMathieu Desnoyers 		}
897a5d8e467SMathieu Desnoyers 	} else {
898a5d8e467SMathieu Desnoyers 		struct debug_obj o = { .object = addr,
899a5d8e467SMathieu Desnoyers 				       .state = ODEBUG_STATE_NOTAVAILABLE,
900a5d8e467SMathieu Desnoyers 				       .descr = descr };
901a5d8e467SMathieu Desnoyers 
902a5d8e467SMathieu Desnoyers 		debug_print_object(&o, "active_state");
903a5d8e467SMathieu Desnoyers 	}
904a5d8e467SMathieu Desnoyers 
905a5d8e467SMathieu Desnoyers 	raw_spin_unlock_irqrestore(&db->lock, flags);
906a5d8e467SMathieu Desnoyers }
907f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_active_state);
908a5d8e467SMathieu Desnoyers 
9093ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
9103ac7fe5aSThomas Gleixner static void __debug_check_no_obj_freed(const void *address, unsigned long size)
9113ac7fe5aSThomas Gleixner {
9123ac7fe5aSThomas Gleixner 	unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
9133ac7fe5aSThomas Gleixner 	struct debug_obj_descr *descr;
9143ac7fe5aSThomas Gleixner 	enum debug_obj_state state;
9153ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
9161ea9b98bSYang Shi 	struct hlist_node *tmp;
9173ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
918bd9dcd04SYang Shi 	int cnt, objs_checked = 0;
9193ac7fe5aSThomas Gleixner 
9203ac7fe5aSThomas Gleixner 	saddr = (unsigned long) address;
9213ac7fe5aSThomas Gleixner 	eaddr = saddr + size;
9223ac7fe5aSThomas Gleixner 	paddr = saddr & ODEBUG_CHUNK_MASK;
9233ac7fe5aSThomas Gleixner 	chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
9243ac7fe5aSThomas Gleixner 	chunks >>= ODEBUG_CHUNK_SHIFT;
9253ac7fe5aSThomas Gleixner 
9263ac7fe5aSThomas Gleixner 	for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
9273ac7fe5aSThomas Gleixner 		db = get_bucket(paddr);
9283ac7fe5aSThomas Gleixner 
9293ac7fe5aSThomas Gleixner repeat:
9303ac7fe5aSThomas Gleixner 		cnt = 0;
931aef9cb05SThomas Gleixner 		raw_spin_lock_irqsave(&db->lock, flags);
932b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
9333ac7fe5aSThomas Gleixner 			cnt++;
9343ac7fe5aSThomas Gleixner 			oaddr = (unsigned long) obj->object;
9353ac7fe5aSThomas Gleixner 			if (oaddr < saddr || oaddr >= eaddr)
9363ac7fe5aSThomas Gleixner 				continue;
9373ac7fe5aSThomas Gleixner 
9383ac7fe5aSThomas Gleixner 			switch (obj->state) {
9393ac7fe5aSThomas Gleixner 			case ODEBUG_STATE_ACTIVE:
9403ac7fe5aSThomas Gleixner 				debug_print_object(obj, "free");
9413ac7fe5aSThomas Gleixner 				descr = obj->descr;
9423ac7fe5aSThomas Gleixner 				state = obj->state;
943aef9cb05SThomas Gleixner 				raw_spin_unlock_irqrestore(&db->lock, flags);
9443ac7fe5aSThomas Gleixner 				debug_object_fixup(descr->fixup_free,
9453ac7fe5aSThomas Gleixner 						   (void *) oaddr, state);
9463ac7fe5aSThomas Gleixner 				goto repeat;
9473ac7fe5aSThomas Gleixner 			default:
9483ac7fe5aSThomas Gleixner 				hlist_del(&obj->node);
949*a7344a68SWaiman Long 				__free_object(obj);
9503ac7fe5aSThomas Gleixner 				break;
9513ac7fe5aSThomas Gleixner 			}
9523ac7fe5aSThomas Gleixner 		}
953aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
954673d62ccSVegard Nossum 
9553ac7fe5aSThomas Gleixner 		if (cnt > debug_objects_maxchain)
9563ac7fe5aSThomas Gleixner 			debug_objects_maxchain = cnt;
957bd9dcd04SYang Shi 
958bd9dcd04SYang Shi 		objs_checked += cnt;
9593ac7fe5aSThomas Gleixner 	}
960bd9dcd04SYang Shi 
961bd9dcd04SYang Shi 	if (objs_checked > debug_objects_maxchecked)
962bd9dcd04SYang Shi 		debug_objects_maxchecked = objs_checked;
9631ea9b98bSYang Shi 
9641ea9b98bSYang Shi 	/* Schedule work to actually kmem_cache_free() objects */
965*a7344a68SWaiman Long 	if (!obj_freeing && obj_nr_tofree) {
966*a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
967*a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
968*a7344a68SWaiman Long 	}
9693ac7fe5aSThomas Gleixner }
9703ac7fe5aSThomas Gleixner 
9713ac7fe5aSThomas Gleixner void debug_check_no_obj_freed(const void *address, unsigned long size)
9723ac7fe5aSThomas Gleixner {
9733ac7fe5aSThomas Gleixner 	if (debug_objects_enabled)
9743ac7fe5aSThomas Gleixner 		__debug_check_no_obj_freed(address, size);
9753ac7fe5aSThomas Gleixner }
9763ac7fe5aSThomas Gleixner #endif
9773ac7fe5aSThomas Gleixner 
9783ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_FS
9793ac7fe5aSThomas Gleixner 
9803ac7fe5aSThomas Gleixner static int debug_stats_show(struct seq_file *m, void *v)
9813ac7fe5aSThomas Gleixner {
982d86998b1SWaiman Long 	int cpu, obj_percpu_free = 0;
983d86998b1SWaiman Long 
984d86998b1SWaiman Long 	for_each_possible_cpu(cpu)
985d86998b1SWaiman Long 		obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu);
986d86998b1SWaiman Long 
9873ac7fe5aSThomas Gleixner 	seq_printf(m, "max_chain     :%d\n", debug_objects_maxchain);
988bd9dcd04SYang Shi 	seq_printf(m, "max_checked   :%d\n", debug_objects_maxchecked);
9893ac7fe5aSThomas Gleixner 	seq_printf(m, "warnings      :%d\n", debug_objects_warnings);
9903ac7fe5aSThomas Gleixner 	seq_printf(m, "fixups        :%d\n", debug_objects_fixups);
991d86998b1SWaiman Long 	seq_printf(m, "pool_free     :%d\n", obj_pool_free + obj_percpu_free);
992d86998b1SWaiman Long 	seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
9933ac7fe5aSThomas Gleixner 	seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
994d86998b1SWaiman Long 	seq_printf(m, "pool_used     :%d\n", obj_pool_used - obj_percpu_free);
9953ac7fe5aSThomas Gleixner 	seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
99636c4ead6SYang Shi 	seq_printf(m, "on_free_list  :%d\n", obj_nr_tofree);
9970cad93c3SWaiman Long 	seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
9980cad93c3SWaiman Long 	seq_printf(m, "objs_freed    :%d\n", debug_objects_freed);
9993ac7fe5aSThomas Gleixner 	return 0;
10003ac7fe5aSThomas Gleixner }
10013ac7fe5aSThomas Gleixner 
10023ac7fe5aSThomas Gleixner static int debug_stats_open(struct inode *inode, struct file *filp)
10033ac7fe5aSThomas Gleixner {
10043ac7fe5aSThomas Gleixner 	return single_open(filp, debug_stats_show, NULL);
10053ac7fe5aSThomas Gleixner }
10063ac7fe5aSThomas Gleixner 
10073ac7fe5aSThomas Gleixner static const struct file_operations debug_stats_fops = {
10083ac7fe5aSThomas Gleixner 	.open		= debug_stats_open,
10093ac7fe5aSThomas Gleixner 	.read		= seq_read,
10103ac7fe5aSThomas Gleixner 	.llseek		= seq_lseek,
10113ac7fe5aSThomas Gleixner 	.release	= single_release,
10123ac7fe5aSThomas Gleixner };
10133ac7fe5aSThomas Gleixner 
10143ac7fe5aSThomas Gleixner static int __init debug_objects_init_debugfs(void)
10153ac7fe5aSThomas Gleixner {
1016fecb0d95SGreg Kroah-Hartman 	struct dentry *dbgdir;
10173ac7fe5aSThomas Gleixner 
10183ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
10193ac7fe5aSThomas Gleixner 		return 0;
10203ac7fe5aSThomas Gleixner 
10213ac7fe5aSThomas Gleixner 	dbgdir = debugfs_create_dir("debug_objects", NULL);
10223ac7fe5aSThomas Gleixner 
1023fecb0d95SGreg Kroah-Hartman 	debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
10243ac7fe5aSThomas Gleixner 
10253ac7fe5aSThomas Gleixner 	return 0;
10263ac7fe5aSThomas Gleixner }
10273ac7fe5aSThomas Gleixner __initcall(debug_objects_init_debugfs);
10283ac7fe5aSThomas Gleixner 
10293ac7fe5aSThomas Gleixner #else
10303ac7fe5aSThomas Gleixner static inline void debug_objects_init_debugfs(void) { }
10313ac7fe5aSThomas Gleixner #endif
10323ac7fe5aSThomas Gleixner 
10333ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
10343ac7fe5aSThomas Gleixner 
10353ac7fe5aSThomas Gleixner /* Random data structure for the self test */
10363ac7fe5aSThomas Gleixner struct self_test {
10373ac7fe5aSThomas Gleixner 	unsigned long	dummy1[6];
10383ac7fe5aSThomas Gleixner 	int		static_init;
10393ac7fe5aSThomas Gleixner 	unsigned long	dummy2[3];
10403ac7fe5aSThomas Gleixner };
10413ac7fe5aSThomas Gleixner 
10423ac7fe5aSThomas Gleixner static __initdata struct debug_obj_descr descr_type_test;
10433ac7fe5aSThomas Gleixner 
1044b9fdac7fSDu, Changbin static bool __init is_static_object(void *addr)
1045b9fdac7fSDu, Changbin {
1046b9fdac7fSDu, Changbin 	struct self_test *obj = addr;
1047b9fdac7fSDu, Changbin 
1048b9fdac7fSDu, Changbin 	return obj->static_init;
1049b9fdac7fSDu, Changbin }
1050b9fdac7fSDu, Changbin 
10513ac7fe5aSThomas Gleixner /*
10523ac7fe5aSThomas Gleixner  * fixup_init is called when:
10533ac7fe5aSThomas Gleixner  * - an active object is initialized
10543ac7fe5aSThomas Gleixner  */
1055b1e4d9d8SDu, Changbin static bool __init fixup_init(void *addr, enum debug_obj_state state)
10563ac7fe5aSThomas Gleixner {
10573ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
10583ac7fe5aSThomas Gleixner 
10593ac7fe5aSThomas Gleixner 	switch (state) {
10603ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
10613ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
10623ac7fe5aSThomas Gleixner 		debug_object_init(obj, &descr_type_test);
1063b1e4d9d8SDu, Changbin 		return true;
10643ac7fe5aSThomas Gleixner 	default:
1065b1e4d9d8SDu, Changbin 		return false;
10663ac7fe5aSThomas Gleixner 	}
10673ac7fe5aSThomas Gleixner }
10683ac7fe5aSThomas Gleixner 
10693ac7fe5aSThomas Gleixner /*
10703ac7fe5aSThomas Gleixner  * fixup_activate is called when:
10713ac7fe5aSThomas Gleixner  * - an active object is activated
1072b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
10733ac7fe5aSThomas Gleixner  */
1074b1e4d9d8SDu, Changbin static bool __init fixup_activate(void *addr, enum debug_obj_state state)
10753ac7fe5aSThomas Gleixner {
10763ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
10773ac7fe5aSThomas Gleixner 
10783ac7fe5aSThomas Gleixner 	switch (state) {
10793ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
1080b1e4d9d8SDu, Changbin 		return true;
10813ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
10823ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
10833ac7fe5aSThomas Gleixner 		debug_object_activate(obj, &descr_type_test);
1084b1e4d9d8SDu, Changbin 		return true;
10853ac7fe5aSThomas Gleixner 
10863ac7fe5aSThomas Gleixner 	default:
1087b1e4d9d8SDu, Changbin 		return false;
10883ac7fe5aSThomas Gleixner 	}
10893ac7fe5aSThomas Gleixner }
10903ac7fe5aSThomas Gleixner 
10913ac7fe5aSThomas Gleixner /*
10923ac7fe5aSThomas Gleixner  * fixup_destroy is called when:
10933ac7fe5aSThomas Gleixner  * - an active object is destroyed
10943ac7fe5aSThomas Gleixner  */
1095b1e4d9d8SDu, Changbin static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
10963ac7fe5aSThomas Gleixner {
10973ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
10983ac7fe5aSThomas Gleixner 
10993ac7fe5aSThomas Gleixner 	switch (state) {
11003ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11013ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11023ac7fe5aSThomas Gleixner 		debug_object_destroy(obj, &descr_type_test);
1103b1e4d9d8SDu, Changbin 		return true;
11043ac7fe5aSThomas Gleixner 	default:
1105b1e4d9d8SDu, Changbin 		return false;
11063ac7fe5aSThomas Gleixner 	}
11073ac7fe5aSThomas Gleixner }
11083ac7fe5aSThomas Gleixner 
11093ac7fe5aSThomas Gleixner /*
11103ac7fe5aSThomas Gleixner  * fixup_free is called when:
11113ac7fe5aSThomas Gleixner  * - an active object is freed
11123ac7fe5aSThomas Gleixner  */
1113b1e4d9d8SDu, Changbin static bool __init fixup_free(void *addr, enum debug_obj_state state)
11143ac7fe5aSThomas Gleixner {
11153ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11163ac7fe5aSThomas Gleixner 
11173ac7fe5aSThomas Gleixner 	switch (state) {
11183ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11193ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11203ac7fe5aSThomas Gleixner 		debug_object_free(obj, &descr_type_test);
1121b1e4d9d8SDu, Changbin 		return true;
11223ac7fe5aSThomas Gleixner 	default:
1123b1e4d9d8SDu, Changbin 		return false;
11243ac7fe5aSThomas Gleixner 	}
11253ac7fe5aSThomas Gleixner }
11263ac7fe5aSThomas Gleixner 
11271fb2f77cSHenrik Kretzschmar static int __init
11283ac7fe5aSThomas Gleixner check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
11293ac7fe5aSThomas Gleixner {
11303ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
11313ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
11323ac7fe5aSThomas Gleixner 	unsigned long flags;
11333ac7fe5aSThomas Gleixner 	int res = -EINVAL;
11343ac7fe5aSThomas Gleixner 
11353ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
11363ac7fe5aSThomas Gleixner 
1137aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
11383ac7fe5aSThomas Gleixner 
11393ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
11403ac7fe5aSThomas Gleixner 	if (!obj && state != ODEBUG_STATE_NONE) {
11415cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
11423ac7fe5aSThomas Gleixner 		goto out;
11433ac7fe5aSThomas Gleixner 	}
11443ac7fe5aSThomas Gleixner 	if (obj && obj->state != state) {
11455cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
11463ac7fe5aSThomas Gleixner 		       obj->state, state);
11473ac7fe5aSThomas Gleixner 		goto out;
11483ac7fe5aSThomas Gleixner 	}
11493ac7fe5aSThomas Gleixner 	if (fixups != debug_objects_fixups) {
11505cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
11513ac7fe5aSThomas Gleixner 		       fixups, debug_objects_fixups);
11523ac7fe5aSThomas Gleixner 		goto out;
11533ac7fe5aSThomas Gleixner 	}
11543ac7fe5aSThomas Gleixner 	if (warnings != debug_objects_warnings) {
11555cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
11563ac7fe5aSThomas Gleixner 		       warnings, debug_objects_warnings);
11573ac7fe5aSThomas Gleixner 		goto out;
11583ac7fe5aSThomas Gleixner 	}
11593ac7fe5aSThomas Gleixner 	res = 0;
11603ac7fe5aSThomas Gleixner out:
1161aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
11623ac7fe5aSThomas Gleixner 	if (res)
11633ac7fe5aSThomas Gleixner 		debug_objects_enabled = 0;
11643ac7fe5aSThomas Gleixner 	return res;
11653ac7fe5aSThomas Gleixner }
11663ac7fe5aSThomas Gleixner 
11673ac7fe5aSThomas Gleixner static __initdata struct debug_obj_descr descr_type_test = {
11683ac7fe5aSThomas Gleixner 	.name			= "selftest",
1169b9fdac7fSDu, Changbin 	.is_static_object	= is_static_object,
11703ac7fe5aSThomas Gleixner 	.fixup_init		= fixup_init,
11713ac7fe5aSThomas Gleixner 	.fixup_activate		= fixup_activate,
11723ac7fe5aSThomas Gleixner 	.fixup_destroy		= fixup_destroy,
11733ac7fe5aSThomas Gleixner 	.fixup_free		= fixup_free,
11743ac7fe5aSThomas Gleixner };
11753ac7fe5aSThomas Gleixner 
11763ac7fe5aSThomas Gleixner static __initdata struct self_test obj = { .static_init = 0 };
11773ac7fe5aSThomas Gleixner 
11783ac7fe5aSThomas Gleixner static void __init debug_objects_selftest(void)
11793ac7fe5aSThomas Gleixner {
11803ac7fe5aSThomas Gleixner 	int fixups, oldfixups, warnings, oldwarnings;
11813ac7fe5aSThomas Gleixner 	unsigned long flags;
11823ac7fe5aSThomas Gleixner 
11833ac7fe5aSThomas Gleixner 	local_irq_save(flags);
11843ac7fe5aSThomas Gleixner 
11853ac7fe5aSThomas Gleixner 	fixups = oldfixups = debug_objects_fixups;
11863ac7fe5aSThomas Gleixner 	warnings = oldwarnings = debug_objects_warnings;
11873ac7fe5aSThomas Gleixner 	descr_test = &descr_type_test;
11883ac7fe5aSThomas Gleixner 
11893ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
11903ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
11913ac7fe5aSThomas Gleixner 		goto out;
11923ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
11933ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
11943ac7fe5aSThomas Gleixner 		goto out;
11953ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
11963ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
11973ac7fe5aSThomas Gleixner 		goto out;
11983ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
11993ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
12003ac7fe5aSThomas Gleixner 		goto out;
12013ac7fe5aSThomas Gleixner 	debug_object_destroy(&obj, &descr_type_test);
12023ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
12033ac7fe5aSThomas Gleixner 		goto out;
12043ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12053ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12063ac7fe5aSThomas Gleixner 		goto out;
12073ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12083ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12093ac7fe5aSThomas Gleixner 		goto out;
12103ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
12113ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12123ac7fe5aSThomas Gleixner 		goto out;
12133ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
12143ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
12153ac7fe5aSThomas Gleixner 		goto out;
12163ac7fe5aSThomas Gleixner 
12173ac7fe5aSThomas Gleixner 	obj.static_init = 1;
12183ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12199f78ff00SStephen Boyd 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
12203ac7fe5aSThomas Gleixner 		goto out;
12213ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12223ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
12233ac7fe5aSThomas Gleixner 		goto out;
12243ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
12253ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
12263ac7fe5aSThomas Gleixner 		goto out;
12273ac7fe5aSThomas Gleixner 
12283ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
12293ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12303ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
12313ac7fe5aSThomas Gleixner 		goto out;
12323ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12333ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
12343ac7fe5aSThomas Gleixner 		goto out;
12353ac7fe5aSThomas Gleixner 	__debug_check_no_obj_freed(&obj, sizeof(obj));
12363ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
12373ac7fe5aSThomas Gleixner 		goto out;
12383ac7fe5aSThomas Gleixner #endif
1239719e4843SFabian Frederick 	pr_info("selftest passed\n");
12403ac7fe5aSThomas Gleixner 
12413ac7fe5aSThomas Gleixner out:
12423ac7fe5aSThomas Gleixner 	debug_objects_fixups = oldfixups;
12433ac7fe5aSThomas Gleixner 	debug_objects_warnings = oldwarnings;
12443ac7fe5aSThomas Gleixner 	descr_test = NULL;
12453ac7fe5aSThomas Gleixner 
12463ac7fe5aSThomas Gleixner 	local_irq_restore(flags);
12473ac7fe5aSThomas Gleixner }
12483ac7fe5aSThomas Gleixner #else
12493ac7fe5aSThomas Gleixner static inline void debug_objects_selftest(void) { }
12503ac7fe5aSThomas Gleixner #endif
12513ac7fe5aSThomas Gleixner 
12523ac7fe5aSThomas Gleixner /*
12533ac7fe5aSThomas Gleixner  * Called during early boot to initialize the hash buckets and link
12543ac7fe5aSThomas Gleixner  * the static object pool objects into the poll list. After this call
12553ac7fe5aSThomas Gleixner  * the object tracker is fully operational.
12563ac7fe5aSThomas Gleixner  */
12573ac7fe5aSThomas Gleixner void __init debug_objects_early_init(void)
12583ac7fe5aSThomas Gleixner {
12593ac7fe5aSThomas Gleixner 	int i;
12603ac7fe5aSThomas Gleixner 
12613ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1262aef9cb05SThomas Gleixner 		raw_spin_lock_init(&obj_hash[i].lock);
12633ac7fe5aSThomas Gleixner 
12643ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i++)
12653ac7fe5aSThomas Gleixner 		hlist_add_head(&obj_static_pool[i].node, &obj_pool);
12663ac7fe5aSThomas Gleixner }
12673ac7fe5aSThomas Gleixner 
12683ac7fe5aSThomas Gleixner /*
12691be1cb7bSThomas Gleixner  * Convert the statically allocated objects to dynamic ones:
12701be1cb7bSThomas Gleixner  */
12711fb2f77cSHenrik Kretzschmar static int __init debug_objects_replace_static_objects(void)
12721be1cb7bSThomas Gleixner {
12731be1cb7bSThomas Gleixner 	struct debug_bucket *db = obj_hash;
1274b67bfe0dSSasha Levin 	struct hlist_node *tmp;
12751be1cb7bSThomas Gleixner 	struct debug_obj *obj, *new;
12761be1cb7bSThomas Gleixner 	HLIST_HEAD(objects);
12771be1cb7bSThomas Gleixner 	int i, cnt = 0;
12781be1cb7bSThomas Gleixner 
12791be1cb7bSThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
12801be1cb7bSThomas Gleixner 		obj = kmem_cache_zalloc(obj_cache, GFP_KERNEL);
12811be1cb7bSThomas Gleixner 		if (!obj)
12821be1cb7bSThomas Gleixner 			goto free;
12831be1cb7bSThomas Gleixner 		hlist_add_head(&obj->node, &objects);
12841be1cb7bSThomas Gleixner 	}
12851be1cb7bSThomas Gleixner 
12861be1cb7bSThomas Gleixner 	/*
1287a9ee3a63SQian Cai 	 * debug_objects_mem_init() is now called early that only one CPU is up
1288a9ee3a63SQian Cai 	 * and interrupts have been disabled, so it is safe to replace the
1289a9ee3a63SQian Cai 	 * active object references.
12901be1cb7bSThomas Gleixner 	 */
12911be1cb7bSThomas Gleixner 
12921be1cb7bSThomas Gleixner 	/* Remove the statically allocated objects from the pool */
1293b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(obj, tmp, &obj_pool, node)
12941be1cb7bSThomas Gleixner 		hlist_del(&obj->node);
12951be1cb7bSThomas Gleixner 	/* Move the allocated objects to the pool */
12961be1cb7bSThomas Gleixner 	hlist_move_list(&objects, &obj_pool);
12971be1cb7bSThomas Gleixner 
12981be1cb7bSThomas Gleixner 	/* Replace the active object references */
12991be1cb7bSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
13001be1cb7bSThomas Gleixner 		hlist_move_list(&db->list, &objects);
13011be1cb7bSThomas Gleixner 
1302b67bfe0dSSasha Levin 		hlist_for_each_entry(obj, &objects, node) {
13031be1cb7bSThomas Gleixner 			new = hlist_entry(obj_pool.first, typeof(*obj), node);
13041be1cb7bSThomas Gleixner 			hlist_del(&new->node);
13051be1cb7bSThomas Gleixner 			/* copy object data */
13061be1cb7bSThomas Gleixner 			*new = *obj;
13071be1cb7bSThomas Gleixner 			hlist_add_head(&new->node, &db->list);
13081be1cb7bSThomas Gleixner 			cnt++;
13091be1cb7bSThomas Gleixner 		}
13101be1cb7bSThomas Gleixner 	}
13111be1cb7bSThomas Gleixner 
1312c0f35cc0SFabian Frederick 	pr_debug("%d of %d active objects replaced\n",
1313c0f35cc0SFabian Frederick 		 cnt, obj_pool_used);
13141be1cb7bSThomas Gleixner 	return 0;
13151be1cb7bSThomas Gleixner free:
1316b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(obj, tmp, &objects, node) {
13171be1cb7bSThomas Gleixner 		hlist_del(&obj->node);
13181be1cb7bSThomas Gleixner 		kmem_cache_free(obj_cache, obj);
13191be1cb7bSThomas Gleixner 	}
13201be1cb7bSThomas Gleixner 	return -ENOMEM;
13211be1cb7bSThomas Gleixner }
13221be1cb7bSThomas Gleixner 
13231be1cb7bSThomas Gleixner /*
13243ac7fe5aSThomas Gleixner  * Called after the kmem_caches are functional to setup a dedicated
13253ac7fe5aSThomas Gleixner  * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
13263ac7fe5aSThomas Gleixner  * prevents that the debug code is called on kmem_cache_free() for the
13273ac7fe5aSThomas Gleixner  * debug tracker objects to avoid recursive calls.
13283ac7fe5aSThomas Gleixner  */
13293ac7fe5aSThomas Gleixner void __init debug_objects_mem_init(void)
13303ac7fe5aSThomas Gleixner {
1331634d61f4SWaiman Long 	int cpu, extras;
1332d86998b1SWaiman Long 
13333ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
13343ac7fe5aSThomas Gleixner 		return;
13353ac7fe5aSThomas Gleixner 
1336d86998b1SWaiman Long 	/*
1337d86998b1SWaiman Long 	 * Initialize the percpu object pools
1338d86998b1SWaiman Long 	 *
1339d86998b1SWaiman Long 	 * Initialization is not strictly necessary, but was done for
1340d86998b1SWaiman Long 	 * completeness.
1341d86998b1SWaiman Long 	 */
1342d86998b1SWaiman Long 	for_each_possible_cpu(cpu)
1343d86998b1SWaiman Long 		INIT_HLIST_HEAD(&per_cpu(percpu_obj_pool.free_objs, cpu));
1344d86998b1SWaiman Long 
13453ac7fe5aSThomas Gleixner 	obj_cache = kmem_cache_create("debug_objects_cache",
13463ac7fe5aSThomas Gleixner 				      sizeof (struct debug_obj), 0,
13478de456cfSQian Cai 				      SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE,
13488de456cfSQian Cai 				      NULL);
13493ac7fe5aSThomas Gleixner 
13501be1cb7bSThomas Gleixner 	if (!obj_cache || debug_objects_replace_static_objects()) {
13513ac7fe5aSThomas Gleixner 		debug_objects_enabled = 0;
13521be1cb7bSThomas Gleixner 		kmem_cache_destroy(obj_cache);
1353719e4843SFabian Frederick 		pr_warn("out of memory.\n");
13541be1cb7bSThomas Gleixner 	} else
13553ac7fe5aSThomas Gleixner 		debug_objects_selftest();
1356634d61f4SWaiman Long 
1357634d61f4SWaiman Long 	/*
1358634d61f4SWaiman Long 	 * Increase the thresholds for allocating and freeing objects
1359634d61f4SWaiman Long 	 * according to the number of possible CPUs available in the system.
1360634d61f4SWaiman Long 	 */
1361634d61f4SWaiman Long 	extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
1362634d61f4SWaiman Long 	debug_objects_pool_size += extras;
1363634d61f4SWaiman Long 	debug_objects_pool_min_level += extras;
13643ac7fe5aSThomas Gleixner }
1365