xref: /linux-6.15/lib/debugobjects.c (revision fb60c004)
19e4a51adSThomas Gleixner // SPDX-License-Identifier: GPL-2.0
23ac7fe5aSThomas Gleixner /*
33ac7fe5aSThomas Gleixner  * Generic infrastructure for lifetime debugging of objects.
43ac7fe5aSThomas Gleixner  *
53ac7fe5aSThomas Gleixner  * Copyright (C) 2008, Thomas Gleixner <[email protected]>
63ac7fe5aSThomas Gleixner  */
7719e4843SFabian Frederick 
8719e4843SFabian Frederick #define pr_fmt(fmt) "ODEBUG: " fmt
9719e4843SFabian Frederick 
103ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
113ac7fe5aSThomas Gleixner #include <linux/interrupt.h>
12d43c36dcSAlexey Dobriyan #include <linux/sched.h>
1368db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
143ac7fe5aSThomas Gleixner #include <linux/seq_file.h>
153ac7fe5aSThomas Gleixner #include <linux/debugfs.h>
165a0e3ad6STejun Heo #include <linux/slab.h>
173ac7fe5aSThomas Gleixner #include <linux/hash.h>
18caba4cbbSWaiman Long #include <linux/kmemleak.h>
1988451f2cSZqiang #include <linux/cpu.h>
203ac7fe5aSThomas Gleixner 
213ac7fe5aSThomas Gleixner #define ODEBUG_HASH_BITS	14
223ac7fe5aSThomas Gleixner #define ODEBUG_HASH_SIZE	(1 << ODEBUG_HASH_BITS)
233ac7fe5aSThomas Gleixner 
240b6ec8c0SChristian Borntraeger #define ODEBUG_POOL_SIZE	1024
253ac7fe5aSThomas Gleixner #define ODEBUG_POOL_MIN_LEVEL	256
26d86998b1SWaiman Long #define ODEBUG_POOL_PERCPU_SIZE	64
27634d61f4SWaiman Long #define ODEBUG_BATCH_SIZE	16
283ac7fe5aSThomas Gleixner 
293ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_SHIFT	PAGE_SHIFT
303ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_SIZE	(1 << ODEBUG_CHUNK_SHIFT)
313ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_MASK	(~(ODEBUG_CHUNK_SIZE - 1))
323ac7fe5aSThomas Gleixner 
33a7344a68SWaiman Long /*
34a7344a68SWaiman Long  * We limit the freeing of debug objects via workqueue at a maximum
35a7344a68SWaiman Long  * frequency of 10Hz and about 1024 objects for each freeing operation.
36a7344a68SWaiman Long  * So it is freeing at most 10k debug objects per second.
37a7344a68SWaiman Long  */
38a7344a68SWaiman Long #define ODEBUG_FREE_WORK_MAX	1024
39a7344a68SWaiman Long #define ODEBUG_FREE_WORK_DELAY	DIV_ROUND_UP(HZ, 10)
40a7344a68SWaiman Long 
413ac7fe5aSThomas Gleixner struct debug_bucket {
423ac7fe5aSThomas Gleixner 	struct hlist_head	list;
43aef9cb05SThomas Gleixner 	raw_spinlock_t		lock;
443ac7fe5aSThomas Gleixner };
453ac7fe5aSThomas Gleixner 
46e18328ffSThomas Gleixner struct obj_pool {
47e18328ffSThomas Gleixner 	struct hlist_head	objects;
48e18328ffSThomas Gleixner 	unsigned int		cnt;
4996a9a042SThomas Gleixner 	unsigned int		min_cnt;
5096a9a042SThomas Gleixner 	unsigned int		max_cnt;
51e18328ffSThomas Gleixner } ____cacheline_aligned;
52e18328ffSThomas Gleixner 
5396a9a042SThomas Gleixner 
5496a9a042SThomas Gleixner static DEFINE_PER_CPU_ALIGNED(struct obj_pool, pool_pcpu)  = {
5596a9a042SThomas Gleixner 	.max_cnt	= ODEBUG_POOL_PERCPU_SIZE,
5696a9a042SThomas Gleixner };
57d86998b1SWaiman Long 
583ac7fe5aSThomas Gleixner static struct debug_bucket	obj_hash[ODEBUG_HASH_SIZE];
593ac7fe5aSThomas Gleixner 
601be1cb7bSThomas Gleixner static struct debug_obj		obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
613ac7fe5aSThomas Gleixner 
62aef9cb05SThomas Gleixner static DEFINE_RAW_SPINLOCK(pool_lock);
633ac7fe5aSThomas Gleixner 
6496a9a042SThomas Gleixner static struct obj_pool pool_global = {
6596a9a042SThomas Gleixner 	.min_cnt	= ODEBUG_POOL_MIN_LEVEL,
6696a9a042SThomas Gleixner 	.max_cnt	= ODEBUG_POOL_SIZE,
6796a9a042SThomas Gleixner };
6896a9a042SThomas Gleixner 
6996a9a042SThomas Gleixner static struct obj_pool pool_to_free = {
7096a9a042SThomas Gleixner 	.max_cnt	= UINT_MAX,
7196a9a042SThomas Gleixner };
723ac7fe5aSThomas Gleixner 
73cb58d190SThomas Gleixner static HLIST_HEAD(pool_boot);
74cb58d190SThomas Gleixner 
75d86998b1SWaiman Long /*
76d86998b1SWaiman Long  * Because of the presence of percpu free pools, obj_pool_free will
77d86998b1SWaiman Long  * under-count those in the percpu free pools. Similarly, obj_pool_used
78d86998b1SWaiman Long  * will over-count those in the percpu free pools. Adjustments will be
79d86998b1SWaiman Long  * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
80d86998b1SWaiman Long  * can be off.
81d86998b1SWaiman Long  */
82e4757c71SZhen Lei static int __data_racy		obj_pool_min_free = ODEBUG_POOL_SIZE;
833ac7fe5aSThomas Gleixner static int			obj_pool_used;
84e4757c71SZhen Lei static int __data_racy		obj_pool_max_used;
85a7344a68SWaiman Long static bool			obj_freeing;
863ac7fe5aSThomas Gleixner 
875b5baba6SBreno Leitao static int __data_racy			debug_objects_maxchain __read_mostly;
885b5baba6SBreno Leitao static int __data_racy __maybe_unused	debug_objects_maxchecked __read_mostly;
895b5baba6SBreno Leitao static int __data_racy			debug_objects_fixups __read_mostly;
905b5baba6SBreno Leitao static int __data_racy			debug_objects_warnings __read_mostly;
91661cc28bSThomas Gleixner static bool __data_racy			debug_objects_enabled __read_mostly
923ae70205SIngo Molnar 					= CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
935b5baba6SBreno Leitao 
94aedcade6SStephen Boyd static const struct debug_obj_descr	*descr_test  __read_mostly;
9568279f9cSAlexey Dobriyan static struct kmem_cache		*obj_cache __ro_after_init;
963ac7fe5aSThomas Gleixner 
97c4b73aabSWaiman Long /*
980cad93c3SWaiman Long  * Track numbers of kmem_cache_alloc()/free() calls done.
99c4b73aabSWaiman Long  */
100e4757c71SZhen Lei static int __data_racy		debug_objects_allocated;
101e4757c71SZhen Lei static int __data_racy		debug_objects_freed;
102c4b73aabSWaiman Long 
103337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work);
104a7344a68SWaiman Long static DECLARE_DELAYED_WORK(debug_obj_work, free_obj_work);
105337fff8bSThomas Gleixner 
1063ac7fe5aSThomas Gleixner static int __init enable_object_debug(char *str)
1073ac7fe5aSThomas Gleixner {
108661cc28bSThomas Gleixner 	debug_objects_enabled = true;
1093ac7fe5aSThomas Gleixner 	return 0;
1103ac7fe5aSThomas Gleixner }
111661cc28bSThomas Gleixner early_param("debug_objects", enable_object_debug);
1123e8ebb5cSKyle McMartin 
1133e8ebb5cSKyle McMartin static int __init disable_object_debug(char *str)
1143e8ebb5cSKyle McMartin {
115661cc28bSThomas Gleixner 	debug_objects_enabled = false;
1163e8ebb5cSKyle McMartin 	return 0;
1173e8ebb5cSKyle McMartin }
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 
129e18328ffSThomas Gleixner static __always_inline unsigned int pool_count(struct obj_pool *pool)
130e18328ffSThomas Gleixner {
131e18328ffSThomas Gleixner 	return READ_ONCE(pool->cnt);
132e18328ffSThomas Gleixner }
133e18328ffSThomas Gleixner 
13496a9a042SThomas Gleixner static __always_inline bool pool_should_refill(struct obj_pool *pool)
135e18328ffSThomas Gleixner {
13696a9a042SThomas Gleixner 	return pool_count(pool) < pool->min_cnt;
137e18328ffSThomas Gleixner }
138e18328ffSThomas Gleixner 
13996a9a042SThomas Gleixner static __always_inline bool pool_must_refill(struct obj_pool *pool)
140e18328ffSThomas Gleixner {
14196a9a042SThomas Gleixner 	return pool_count(pool) < pool->min_cnt / 2;
142e18328ffSThomas Gleixner }
143e18328ffSThomas Gleixner 
144*fb60c004SThomas Gleixner static bool pool_move_batch(struct obj_pool *dst, struct obj_pool *src)
145*fb60c004SThomas Gleixner {
146*fb60c004SThomas Gleixner 	if (dst->cnt + ODEBUG_BATCH_SIZE > dst->max_cnt || !src->cnt)
147*fb60c004SThomas Gleixner 		return false;
148*fb60c004SThomas Gleixner 
149*fb60c004SThomas Gleixner 	for (int i = 0; i < ODEBUG_BATCH_SIZE && src->cnt; i++) {
150*fb60c004SThomas Gleixner 		struct hlist_node *node = src->objects.first;
151*fb60c004SThomas Gleixner 
152*fb60c004SThomas Gleixner 		WRITE_ONCE(src->cnt, src->cnt - 1);
153*fb60c004SThomas Gleixner 		WRITE_ONCE(dst->cnt, dst->cnt + 1);
154*fb60c004SThomas Gleixner 
155*fb60c004SThomas Gleixner 		hlist_del(node);
156*fb60c004SThomas Gleixner 		hlist_add_head(node, &dst->objects);
157*fb60c004SThomas Gleixner 	}
158*fb60c004SThomas Gleixner 	return true;
159*fb60c004SThomas Gleixner }
160*fb60c004SThomas Gleixner 
161*fb60c004SThomas Gleixner static struct debug_obj *__alloc_object(struct hlist_head *list)
162*fb60c004SThomas Gleixner {
163*fb60c004SThomas Gleixner 	struct debug_obj *obj;
164*fb60c004SThomas Gleixner 
165*fb60c004SThomas Gleixner 	if (unlikely(!list->first))
166*fb60c004SThomas Gleixner 		return NULL;
167*fb60c004SThomas Gleixner 
168*fb60c004SThomas Gleixner 	obj = hlist_entry(list->first, typeof(*obj), node);
169*fb60c004SThomas Gleixner 	hlist_del(&obj->node);
170*fb60c004SThomas Gleixner 	return obj;
171*fb60c004SThomas Gleixner }
172*fb60c004SThomas Gleixner 
173*fb60c004SThomas Gleixner static struct debug_obj *pcpu_alloc(void)
174*fb60c004SThomas Gleixner {
175*fb60c004SThomas Gleixner 	struct obj_pool *pcp = this_cpu_ptr(&pool_pcpu);
176*fb60c004SThomas Gleixner 
177*fb60c004SThomas Gleixner 	lockdep_assert_irqs_disabled();
178*fb60c004SThomas Gleixner 
179*fb60c004SThomas Gleixner 	for (;;) {
180*fb60c004SThomas Gleixner 		struct debug_obj *obj = __alloc_object(&pcp->objects);
181*fb60c004SThomas Gleixner 
182*fb60c004SThomas Gleixner 		if (likely(obj)) {
183*fb60c004SThomas Gleixner 			pcp->cnt--;
184*fb60c004SThomas Gleixner 			return obj;
185*fb60c004SThomas Gleixner 		}
186*fb60c004SThomas Gleixner 
187*fb60c004SThomas Gleixner 		guard(raw_spinlock)(&pool_lock);
188*fb60c004SThomas Gleixner 		if (!pool_move_batch(pcp, &pool_to_free)) {
189*fb60c004SThomas Gleixner 			if (!pool_move_batch(pcp, &pool_global))
190*fb60c004SThomas Gleixner 				return NULL;
191*fb60c004SThomas Gleixner 		}
192*fb60c004SThomas Gleixner 		obj_pool_used += pcp->cnt;
193*fb60c004SThomas Gleixner 
194*fb60c004SThomas Gleixner 		if (obj_pool_used > obj_pool_max_used)
195*fb60c004SThomas Gleixner 			obj_pool_max_used = obj_pool_used;
196*fb60c004SThomas Gleixner 
197*fb60c004SThomas Gleixner 		if (pool_global.cnt < obj_pool_min_free)
198*fb60c004SThomas Gleixner 			obj_pool_min_free = pool_global.cnt;
199*fb60c004SThomas Gleixner 	}
200*fb60c004SThomas Gleixner }
201*fb60c004SThomas Gleixner 
20249a5cb82SThomas Gleixner static void free_object_list(struct hlist_head *head)
20349a5cb82SThomas Gleixner {
20449a5cb82SThomas Gleixner 	struct hlist_node *tmp;
20549a5cb82SThomas Gleixner 	struct debug_obj *obj;
20649a5cb82SThomas Gleixner 	int cnt = 0;
20749a5cb82SThomas Gleixner 
20849a5cb82SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, head, node) {
20949a5cb82SThomas Gleixner 		hlist_del(&obj->node);
21049a5cb82SThomas Gleixner 		kmem_cache_free(obj_cache, obj);
21149a5cb82SThomas Gleixner 		cnt++;
21249a5cb82SThomas Gleixner 	}
21349a5cb82SThomas Gleixner 	debug_objects_freed += cnt;
21449a5cb82SThomas Gleixner }
21549a5cb82SThomas Gleixner 
216d8c6cd3aSZhen Lei static void fill_pool_from_freelist(void)
2173ac7fe5aSThomas Gleixner {
218d8c6cd3aSZhen Lei 	static unsigned long state;
2193ac7fe5aSThomas Gleixner 
22036c4ead6SYang Shi 	/*
22163a4a9b5SZhen Lei 	 * Reuse objs from the global obj_to_free list; they will be
22263a4a9b5SZhen Lei 	 * reinitialized when allocating.
22336c4ead6SYang Shi 	 */
224e18328ffSThomas Gleixner 	if (!pool_count(&pool_to_free))
225d8c6cd3aSZhen Lei 		return;
226d8c6cd3aSZhen Lei 
227d8c6cd3aSZhen Lei 	/*
228d8c6cd3aSZhen Lei 	 * Prevent the context from being scheduled or interrupted after
229d8c6cd3aSZhen Lei 	 * setting the state flag;
230d8c6cd3aSZhen Lei 	 */
231d8c6cd3aSZhen Lei 	guard(irqsave)();
232d8c6cd3aSZhen Lei 
233d8c6cd3aSZhen Lei 	/*
234d8c6cd3aSZhen Lei 	 * Avoid lock contention on &pool_lock and avoid making the cache
235d8c6cd3aSZhen Lei 	 * line exclusive by testing the bit before attempting to set it.
236d8c6cd3aSZhen Lei 	 */
237d8c6cd3aSZhen Lei 	if (test_bit(0, &state) || test_and_set_bit(0, &state))
238d8c6cd3aSZhen Lei 		return;
239d8c6cd3aSZhen Lei 
240*fb60c004SThomas Gleixner 	/* Avoid taking the lock when there is no work to do */
241*fb60c004SThomas Gleixner 	while (pool_should_refill(&pool_global) && pool_count(&pool_to_free)) {
242d8c6cd3aSZhen Lei 		guard(raw_spinlock)(&pool_lock);
243*fb60c004SThomas Gleixner 		/* Move a batch if possible */
244*fb60c004SThomas Gleixner 		pool_move_batch(&pool_global, &pool_to_free);
24536c4ead6SYang Shi 	}
246d8c6cd3aSZhen Lei 	clear_bit(0, &state);
24736c4ead6SYang Shi }
24836c4ead6SYang Shi 
249d8c6cd3aSZhen Lei static void fill_pool(void)
250d8c6cd3aSZhen Lei {
251d8c6cd3aSZhen Lei 	static atomic_t cpus_allocating;
252d8c6cd3aSZhen Lei 
253d8c6cd3aSZhen Lei 	/*
254d8c6cd3aSZhen Lei 	 * Avoid allocation and lock contention when:
255d8c6cd3aSZhen Lei 	 *   - One other CPU is already allocating
256d8c6cd3aSZhen Lei 	 *   - the global pool has not reached the critical level yet
257d8c6cd3aSZhen Lei 	 */
25896a9a042SThomas Gleixner 	if (!pool_must_refill(&pool_global) && atomic_read(&cpus_allocating))
2591fda107dSThomas Gleixner 		return;
2603ac7fe5aSThomas Gleixner 
261d8c6cd3aSZhen Lei 	atomic_inc(&cpus_allocating);
26296a9a042SThomas Gleixner 	while (pool_should_refill(&pool_global)) {
263813fd078SZhen Lei 		struct debug_obj *new, *last = NULL;
264813fd078SZhen Lei 		HLIST_HEAD(head);
265d26bf505SWaiman Long 		int cnt;
2663ac7fe5aSThomas Gleixner 
267d26bf505SWaiman Long 		for (cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) {
268d8c6cd3aSZhen Lei 			new = kmem_cache_zalloc(obj_cache, __GFP_HIGH | __GFP_NOWARN);
269813fd078SZhen Lei 			if (!new)
270d26bf505SWaiman Long 				break;
271813fd078SZhen Lei 			hlist_add_head(&new->node, &head);
272813fd078SZhen Lei 			if (!last)
273813fd078SZhen Lei 				last = new;
274d26bf505SWaiman Long 		}
275d26bf505SWaiman Long 		if (!cnt)
276d8c6cd3aSZhen Lei 			break;
2773ac7fe5aSThomas Gleixner 
278d8c6cd3aSZhen Lei 		guard(raw_spinlock_irqsave)(&pool_lock);
279e18328ffSThomas Gleixner 		hlist_splice_init(&head, &last->node, &pool_global.objects);
280813fd078SZhen Lei 		debug_objects_allocated += cnt;
281e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + cnt);
2823ac7fe5aSThomas Gleixner 	}
283d8c6cd3aSZhen Lei 	atomic_dec(&cpus_allocating);
2843ac7fe5aSThomas Gleixner }
2853ac7fe5aSThomas Gleixner 
2863ac7fe5aSThomas Gleixner /*
2873ac7fe5aSThomas Gleixner  * Lookup an object in the hash bucket.
2883ac7fe5aSThomas Gleixner  */
2893ac7fe5aSThomas Gleixner static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
2903ac7fe5aSThomas Gleixner {
2913ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
2923ac7fe5aSThomas Gleixner 	int cnt = 0;
2933ac7fe5aSThomas Gleixner 
294b67bfe0dSSasha Levin 	hlist_for_each_entry(obj, &b->list, node) {
2953ac7fe5aSThomas Gleixner 		cnt++;
2963ac7fe5aSThomas Gleixner 		if (obj->object == addr)
2973ac7fe5aSThomas Gleixner 			return obj;
2983ac7fe5aSThomas Gleixner 	}
2993ac7fe5aSThomas Gleixner 	if (cnt > debug_objects_maxchain)
3003ac7fe5aSThomas Gleixner 		debug_objects_maxchain = cnt;
3013ac7fe5aSThomas Gleixner 
3023ac7fe5aSThomas Gleixner 	return NULL;
3033ac7fe5aSThomas Gleixner }
3043ac7fe5aSThomas Gleixner 
305*fb60c004SThomas Gleixner static struct debug_obj *alloc_object(void *addr, struct debug_bucket *b,
306*fb60c004SThomas Gleixner 				      const struct debug_obj_descr *descr)
307d86998b1SWaiman Long {
308d86998b1SWaiman Long 	struct debug_obj *obj;
309d86998b1SWaiman Long 
310*fb60c004SThomas Gleixner 	if (likely(obj_cache))
311*fb60c004SThomas Gleixner 		obj = pcpu_alloc();
312*fb60c004SThomas Gleixner 	else
313cb58d190SThomas Gleixner 		obj = __alloc_object(&pool_boot);
3143ac7fe5aSThomas Gleixner 
315*fb60c004SThomas Gleixner 	if (likely(obj)) {
316d86998b1SWaiman Long 		obj->object = addr;
317d86998b1SWaiman Long 		obj->descr  = descr;
318d86998b1SWaiman Long 		obj->state  = ODEBUG_STATE_NONE;
319d86998b1SWaiman Long 		obj->astate = 0;
320d86998b1SWaiman Long 		hlist_add_head(&obj->node, &b->list);
321d86998b1SWaiman Long 	}
3223ac7fe5aSThomas Gleixner 	return obj;
3233ac7fe5aSThomas Gleixner }
3243ac7fe5aSThomas Gleixner 
3253ac7fe5aSThomas Gleixner /*
326337fff8bSThomas Gleixner  * workqueue function to free objects.
327858274b6SWaiman Long  *
328858274b6SWaiman Long  * To reduce contention on the global pool_lock, the actual freeing of
329636e1970SYang Shi  * debug objects will be delayed if the pool_lock is busy.
330337fff8bSThomas Gleixner  */
331337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work)
332337fff8bSThomas Gleixner {
33336c4ead6SYang Shi 	struct debug_obj *obj;
334337fff8bSThomas Gleixner 	unsigned long flags;
33536c4ead6SYang Shi 	HLIST_HEAD(tofree);
336337fff8bSThomas Gleixner 
337a7344a68SWaiman Long 	WRITE_ONCE(obj_freeing, false);
338858274b6SWaiman Long 	if (!raw_spin_trylock_irqsave(&pool_lock, flags))
339858274b6SWaiman Long 		return;
34036c4ead6SYang Shi 
34196a9a042SThomas Gleixner 	if (pool_global.cnt >= pool_global.max_cnt)
342a7344a68SWaiman Long 		goto free_objs;
343a7344a68SWaiman Long 
34436c4ead6SYang Shi 	/*
34536c4ead6SYang Shi 	 * The objs on the pool list might be allocated before the work is
34636c4ead6SYang Shi 	 * run, so recheck if pool list it full or not, if not fill pool
347a7344a68SWaiman Long 	 * list from the global free list. As it is likely that a workload
348a7344a68SWaiman Long 	 * may be gearing up to use more and more objects, don't free any
349a7344a68SWaiman Long 	 * of them until the next round.
35036c4ead6SYang Shi 	 */
35196a9a042SThomas Gleixner 	while (pool_to_free.cnt && pool_global.cnt < pool_global.max_cnt) {
352e18328ffSThomas Gleixner 		obj = hlist_entry(pool_to_free.objects.first, typeof(*obj), node);
35336c4ead6SYang Shi 		hlist_del(&obj->node);
354e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_global.objects);
355e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt - 1);
356e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + 1);
35736c4ead6SYang Shi 	}
358a7344a68SWaiman Long 	raw_spin_unlock_irqrestore(&pool_lock, flags);
359a7344a68SWaiman Long 	return;
36036c4ead6SYang Shi 
361a7344a68SWaiman Long free_objs:
36236c4ead6SYang Shi 	/*
36336c4ead6SYang Shi 	 * Pool list is already full and there are still objs on the free
36436c4ead6SYang Shi 	 * list. Move remaining free objs to a temporary list to free the
36536c4ead6SYang Shi 	 * memory outside the pool_lock held region.
36636c4ead6SYang Shi 	 */
367e18328ffSThomas Gleixner 	if (pool_to_free.cnt) {
368e18328ffSThomas Gleixner 		hlist_move_list(&pool_to_free.objects, &tofree);
369e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, 0);
37036c4ead6SYang Shi 	}
371aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&pool_lock, flags);
37236c4ead6SYang Shi 
37349a5cb82SThomas Gleixner 	free_object_list(&tofree);
374337fff8bSThomas Gleixner }
375337fff8bSThomas Gleixner 
376a7344a68SWaiman Long static void __free_object(struct debug_obj *obj)
377636e1970SYang Shi {
378634d61f4SWaiman Long 	struct debug_obj *objs[ODEBUG_BATCH_SIZE];
37918b8afcbSThomas Gleixner 	struct obj_pool *percpu_pool;
380634d61f4SWaiman Long 	int lookahead_count = 0;
381636e1970SYang Shi 	bool work;
382636e1970SYang Shi 
383cb58d190SThomas Gleixner 	guard(irqsave)();
384cb58d190SThomas Gleixner 
385cb58d190SThomas Gleixner 	if (unlikely(!obj_cache)) {
386cb58d190SThomas Gleixner 		hlist_add_head(&obj->node, &pool_boot);
387cb58d190SThomas Gleixner 		return;
388cb58d190SThomas Gleixner 	}
389634d61f4SWaiman Long 
390d86998b1SWaiman Long 	/*
391d86998b1SWaiman Long 	 * Try to free it into the percpu pool first.
392d86998b1SWaiman Long 	 */
39318b8afcbSThomas Gleixner 	percpu_pool = this_cpu_ptr(&pool_pcpu);
39418b8afcbSThomas Gleixner 	if (percpu_pool->cnt < ODEBUG_POOL_PERCPU_SIZE) {
39518b8afcbSThomas Gleixner 		hlist_add_head(&obj->node, &percpu_pool->objects);
39618b8afcbSThomas Gleixner 		percpu_pool->cnt++;
397a7344a68SWaiman Long 		return;
398d86998b1SWaiman Long 	}
399d86998b1SWaiman Long 
400634d61f4SWaiman Long 	/*
401634d61f4SWaiman Long 	 * As the percpu pool is full, look ahead and pull out a batch
402634d61f4SWaiman Long 	 * of objects from the percpu pool and free them as well.
403634d61f4SWaiman Long 	 */
404634d61f4SWaiman Long 	for (; lookahead_count < ODEBUG_BATCH_SIZE; lookahead_count++) {
40518b8afcbSThomas Gleixner 		objs[lookahead_count] = __alloc_object(&percpu_pool->objects);
406634d61f4SWaiman Long 		if (!objs[lookahead_count])
407634d61f4SWaiman Long 			break;
40818b8afcbSThomas Gleixner 		percpu_pool->cnt--;
409634d61f4SWaiman Long 	}
410634d61f4SWaiman Long 
411d86998b1SWaiman Long 	raw_spin_lock(&pool_lock);
41296a9a042SThomas Gleixner 	work = (pool_global.cnt > pool_global.max_cnt) && obj_cache &&
413e18328ffSThomas Gleixner 	       (pool_to_free.cnt < ODEBUG_FREE_WORK_MAX);
414636e1970SYang Shi 	obj_pool_used--;
415636e1970SYang Shi 
416636e1970SYang Shi 	if (work) {
417e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt + 1);
418e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_to_free.objects);
419634d61f4SWaiman Long 		if (lookahead_count) {
420e18328ffSThomas Gleixner 			WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt + lookahead_count);
421634d61f4SWaiman Long 			obj_pool_used -= lookahead_count;
422634d61f4SWaiman Long 			while (lookahead_count) {
423634d61f4SWaiman Long 				hlist_add_head(&objs[--lookahead_count]->node,
424e18328ffSThomas Gleixner 					       &pool_to_free.objects);
425634d61f4SWaiman Long 			}
426634d61f4SWaiman Long 		}
427a7344a68SWaiman Long 
42896a9a042SThomas Gleixner 		if ((pool_global.cnt > pool_global.max_cnt) &&
429e18328ffSThomas Gleixner 		    (pool_to_free.cnt < ODEBUG_FREE_WORK_MAX)) {
430a7344a68SWaiman Long 			int i;
431a7344a68SWaiman Long 
432a7344a68SWaiman Long 			/*
433a7344a68SWaiman Long 			 * Free one more batch of objects from obj_pool.
434a7344a68SWaiman Long 			 */
435a7344a68SWaiman Long 			for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
436e18328ffSThomas Gleixner 				obj = __alloc_object(&pool_global.objects);
437e18328ffSThomas Gleixner 				hlist_add_head(&obj->node, &pool_to_free.objects);
438e18328ffSThomas Gleixner 				WRITE_ONCE(pool_global.cnt, pool_global.cnt - 1);
439e18328ffSThomas Gleixner 				WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt + 1);
440a7344a68SWaiman Long 			}
441a7344a68SWaiman Long 		}
442636e1970SYang Shi 	} else {
443e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + 1);
444e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_global.objects);
445634d61f4SWaiman Long 		if (lookahead_count) {
446e18328ffSThomas Gleixner 			WRITE_ONCE(pool_global.cnt, pool_global.cnt + lookahead_count);
447634d61f4SWaiman Long 			obj_pool_used -= lookahead_count;
448634d61f4SWaiman Long 			while (lookahead_count) {
449634d61f4SWaiman Long 				hlist_add_head(&objs[--lookahead_count]->node,
450e18328ffSThomas Gleixner 					       &pool_global.objects);
451634d61f4SWaiman Long 			}
452634d61f4SWaiman Long 		}
453636e1970SYang Shi 	}
454d86998b1SWaiman Long 	raw_spin_unlock(&pool_lock);
455636e1970SYang Shi }
456636e1970SYang Shi 
457337fff8bSThomas Gleixner /*
458337fff8bSThomas Gleixner  * Put the object back into the pool and schedule work to free objects
459337fff8bSThomas Gleixner  * if necessary.
4603ac7fe5aSThomas Gleixner  */
4613ac7fe5aSThomas Gleixner static void free_object(struct debug_obj *obj)
4623ac7fe5aSThomas Gleixner {
463a7344a68SWaiman Long 	__free_object(obj);
464e18328ffSThomas Gleixner 	if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
465a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
466a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
467a7344a68SWaiman Long 	}
4683ac7fe5aSThomas Gleixner }
4693ac7fe5aSThomas Gleixner 
470a2a70238SThomas Gleixner static void put_objects(struct hlist_head *list)
47188451f2cSZqiang {
47288451f2cSZqiang 	struct hlist_node *tmp;
47388451f2cSZqiang 	struct debug_obj *obj;
47488451f2cSZqiang 
475a2a70238SThomas Gleixner 	/*
476a2a70238SThomas Gleixner 	 * Using free_object() puts the objects into reuse or schedules
477a2a70238SThomas Gleixner 	 * them for freeing and it get's all the accounting correct.
478a2a70238SThomas Gleixner 	 */
479a2a70238SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, list, node) {
48088451f2cSZqiang 		hlist_del(&obj->node);
481a2a70238SThomas Gleixner 		free_object(obj);
482a2a70238SThomas Gleixner 	}
48388451f2cSZqiang }
484eabb7f1aSwuchi 
48549968cf1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
486a2a70238SThomas Gleixner static int object_cpu_offline(unsigned int cpu)
487a2a70238SThomas Gleixner {
488a2a70238SThomas Gleixner 	/* Remote access is safe as the CPU is dead already */
48918b8afcbSThomas Gleixner 	struct obj_pool *pcp = per_cpu_ptr(&pool_pcpu, cpu);
490eabb7f1aSwuchi 
49118b8afcbSThomas Gleixner 	put_objects(&pcp->objects);
49218b8afcbSThomas Gleixner 	pcp->cnt = 0;
49388451f2cSZqiang 	return 0;
49488451f2cSZqiang }
49588451f2cSZqiang #endif
49688451f2cSZqiang 
49749968cf1SThomas Gleixner /* Out of memory. Free all objects from hash */
4983ac7fe5aSThomas Gleixner static void debug_objects_oom(void)
4993ac7fe5aSThomas Gleixner {
5003ac7fe5aSThomas Gleixner 	struct debug_bucket *db = obj_hash;
501673d62ccSVegard Nossum 	HLIST_HEAD(freelist);
5023ac7fe5aSThomas Gleixner 
503719e4843SFabian Frederick 	pr_warn("Out of memory. ODEBUG disabled\n");
5043ac7fe5aSThomas Gleixner 
50549968cf1SThomas Gleixner 	for (int i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
50649968cf1SThomas Gleixner 		scoped_guard(raw_spinlock_irqsave, &db->lock)
507673d62ccSVegard Nossum 			hlist_move_list(&db->list, &freelist);
508673d62ccSVegard Nossum 
50949968cf1SThomas Gleixner 		put_objects(&freelist);
5103ac7fe5aSThomas Gleixner 	}
5113ac7fe5aSThomas Gleixner }
5123ac7fe5aSThomas Gleixner 
5133ac7fe5aSThomas Gleixner /*
5143ac7fe5aSThomas Gleixner  * We use the pfn of the address for the hash. That way we can check
5153ac7fe5aSThomas Gleixner  * for freed objects simply by checking the affected bucket.
5163ac7fe5aSThomas Gleixner  */
5173ac7fe5aSThomas Gleixner static struct debug_bucket *get_bucket(unsigned long addr)
5183ac7fe5aSThomas Gleixner {
5193ac7fe5aSThomas Gleixner 	unsigned long hash;
5203ac7fe5aSThomas Gleixner 
5213ac7fe5aSThomas Gleixner 	hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
5223ac7fe5aSThomas Gleixner 	return &obj_hash[hash];
5233ac7fe5aSThomas Gleixner }
5243ac7fe5aSThomas Gleixner 
5253ac7fe5aSThomas Gleixner static void debug_print_object(struct debug_obj *obj, char *msg)
5263ac7fe5aSThomas Gleixner {
527aedcade6SStephen Boyd 	const struct debug_obj_descr *descr = obj->descr;
5283ac7fe5aSThomas Gleixner 	static int limit;
5293ac7fe5aSThomas Gleixner 
5308b64d420STetsuo Handa 	/*
5318b64d420STetsuo Handa 	 * Don't report if lookup_object_or_alloc() by the current thread
5328b64d420STetsuo Handa 	 * failed because lookup_object_or_alloc()/debug_objects_oom() by a
5338b64d420STetsuo Handa 	 * concurrent thread turned off debug_objects_enabled and cleared
5348b64d420STetsuo Handa 	 * the hash buckets.
5358b64d420STetsuo Handa 	 */
5368b64d420STetsuo Handa 	if (!debug_objects_enabled)
5378b64d420STetsuo Handa 		return;
5388b64d420STetsuo Handa 
53999777288SStanislaw Gruszka 	if (limit < 5 && descr != descr_test) {
54099777288SStanislaw Gruszka 		void *hint = descr->debug_hint ?
54199777288SStanislaw Gruszka 			descr->debug_hint(obj->object) : NULL;
5423ac7fe5aSThomas Gleixner 		limit++;
543a5d8e467SMathieu Desnoyers 		WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
544c4db2d3bSStephen Boyd 				 "object: %p object type: %s hint: %pS\n",
545a5d8e467SMathieu Desnoyers 			msg, obj_states[obj->state], obj->astate,
546c4db2d3bSStephen Boyd 			obj->object, descr->name, hint);
5473ac7fe5aSThomas Gleixner 	}
5483ac7fe5aSThomas Gleixner 	debug_objects_warnings++;
5493ac7fe5aSThomas Gleixner }
5503ac7fe5aSThomas Gleixner 
5513ac7fe5aSThomas Gleixner /*
5523ac7fe5aSThomas Gleixner  * Try to repair the damage, so we have a better chance to get useful
5533ac7fe5aSThomas Gleixner  * debug output.
5543ac7fe5aSThomas Gleixner  */
555b1e4d9d8SDu, Changbin static bool
556b1e4d9d8SDu, Changbin debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
5573ac7fe5aSThomas Gleixner 		   void * addr, enum debug_obj_state state)
5583ac7fe5aSThomas Gleixner {
559b1e4d9d8SDu, Changbin 	if (fixup && fixup(addr, state)) {
560b1e4d9d8SDu, Changbin 		debug_objects_fixups++;
561b1e4d9d8SDu, Changbin 		return true;
562b1e4d9d8SDu, Changbin 	}
563b1e4d9d8SDu, Changbin 	return false;
5643ac7fe5aSThomas Gleixner }
5653ac7fe5aSThomas Gleixner 
5663ac7fe5aSThomas Gleixner static void debug_object_is_on_stack(void *addr, int onstack)
5673ac7fe5aSThomas Gleixner {
5683ac7fe5aSThomas Gleixner 	int is_on_stack;
5693ac7fe5aSThomas Gleixner 	static int limit;
5703ac7fe5aSThomas Gleixner 
5713ac7fe5aSThomas Gleixner 	if (limit > 4)
5723ac7fe5aSThomas Gleixner 		return;
5733ac7fe5aSThomas Gleixner 
5748b05c7e6SFUJITA Tomonori 	is_on_stack = object_is_on_stack(addr);
5753ac7fe5aSThomas Gleixner 	if (is_on_stack == onstack)
5763ac7fe5aSThomas Gleixner 		return;
5773ac7fe5aSThomas Gleixner 
5783ac7fe5aSThomas Gleixner 	limit++;
5793ac7fe5aSThomas Gleixner 	if (is_on_stack)
580fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
581fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
5823ac7fe5aSThomas Gleixner 	else
583fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
584fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
585fc91a3c4SJoel Fernandes (Google) 
5863ac7fe5aSThomas Gleixner 	WARN_ON(1);
5873ac7fe5aSThomas Gleixner }
5883ac7fe5aSThomas Gleixner 
58963a75969SThomas Gleixner static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
59063a75969SThomas Gleixner 						const struct debug_obj_descr *descr,
59163a75969SThomas Gleixner 						bool onstack, bool alloc_ifstatic)
59263a75969SThomas Gleixner {
59363a75969SThomas Gleixner 	struct debug_obj *obj = lookup_object(addr, b);
59463a75969SThomas Gleixner 	enum debug_obj_state state = ODEBUG_STATE_NONE;
59563a75969SThomas Gleixner 
59663a75969SThomas Gleixner 	if (likely(obj))
59763a75969SThomas Gleixner 		return obj;
59863a75969SThomas Gleixner 
59963a75969SThomas Gleixner 	/*
60063a75969SThomas Gleixner 	 * debug_object_init() unconditionally allocates untracked
60163a75969SThomas Gleixner 	 * objects. It does not matter whether it is a static object or
60263a75969SThomas Gleixner 	 * not.
60363a75969SThomas Gleixner 	 *
60463a75969SThomas Gleixner 	 * debug_object_assert_init() and debug_object_activate() allow
60563a75969SThomas Gleixner 	 * allocation only if the descriptor callback confirms that the
60663a75969SThomas Gleixner 	 * object is static and considered initialized. For non-static
60763a75969SThomas Gleixner 	 * objects the allocation needs to be done from the fixup callback.
60863a75969SThomas Gleixner 	 */
60963a75969SThomas Gleixner 	if (unlikely(alloc_ifstatic)) {
61063a75969SThomas Gleixner 		if (!descr->is_static_object || !descr->is_static_object(addr))
61163a75969SThomas Gleixner 			return ERR_PTR(-ENOENT);
61263a75969SThomas Gleixner 		/* Statically allocated objects are considered initialized */
61363a75969SThomas Gleixner 		state = ODEBUG_STATE_INIT;
61463a75969SThomas Gleixner 	}
61563a75969SThomas Gleixner 
61663a75969SThomas Gleixner 	obj = alloc_object(addr, b, descr);
61763a75969SThomas Gleixner 	if (likely(obj)) {
61863a75969SThomas Gleixner 		obj->state = state;
61963a75969SThomas Gleixner 		debug_object_is_on_stack(addr, onstack);
62063a75969SThomas Gleixner 		return obj;
62163a75969SThomas Gleixner 	}
62263a75969SThomas Gleixner 
62363a75969SThomas Gleixner 	/* Out of memory. Do the cleanup outside of the locked region */
624661cc28bSThomas Gleixner 	debug_objects_enabled = false;
62563a75969SThomas Gleixner 	return NULL;
62663a75969SThomas Gleixner }
62763a75969SThomas Gleixner 
6280af462f1SThomas Gleixner static void debug_objects_fill_pool(void)
6290af462f1SThomas Gleixner {
630d8c6cd3aSZhen Lei 	if (unlikely(!obj_cache))
631d8c6cd3aSZhen Lei 		return;
632d8c6cd3aSZhen Lei 
63396a9a042SThomas Gleixner 	if (likely(!pool_should_refill(&pool_global)))
634d8c6cd3aSZhen Lei 		return;
635d8c6cd3aSZhen Lei 
636d8c6cd3aSZhen Lei 	/* Try reusing objects from obj_to_free_list */
637d8c6cd3aSZhen Lei 	fill_pool_from_freelist();
638d8c6cd3aSZhen Lei 
63996a9a042SThomas Gleixner 	if (likely(!pool_should_refill(&pool_global)))
640d8c6cd3aSZhen Lei 		return;
641d8c6cd3aSZhen Lei 
6420af462f1SThomas Gleixner 	/*
6430af462f1SThomas Gleixner 	 * On RT enabled kernels the pool refill must happen in preemptible
6440cce06baSPeter Zijlstra 	 * context -- for !RT kernels we rely on the fact that spinlock_t and
6450cce06baSPeter Zijlstra 	 * raw_spinlock_t are basically the same type and this lock-type
6460cce06baSPeter Zijlstra 	 * inversion works just fine.
6470af462f1SThomas Gleixner 	 */
6480cce06baSPeter Zijlstra 	if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible()) {
6490cce06baSPeter Zijlstra 		/*
6500cce06baSPeter Zijlstra 		 * Annotate away the spinlock_t inside raw_spinlock_t warning
6510cce06baSPeter Zijlstra 		 * by temporarily raising the wait-type to WAIT_SLEEP, matching
6520cce06baSPeter Zijlstra 		 * the preemptible() condition above.
6530cce06baSPeter Zijlstra 		 */
6540cce06baSPeter Zijlstra 		static DEFINE_WAIT_OVERRIDE_MAP(fill_pool_map, LD_WAIT_SLEEP);
6550cce06baSPeter Zijlstra 		lock_map_acquire_try(&fill_pool_map);
6560af462f1SThomas Gleixner 		fill_pool();
6570cce06baSPeter Zijlstra 		lock_map_release(&fill_pool_map);
6580cce06baSPeter Zijlstra 	}
6590af462f1SThomas Gleixner }
6600af462f1SThomas Gleixner 
6613ac7fe5aSThomas Gleixner static void
662aedcade6SStephen Boyd __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
6633ac7fe5aSThomas Gleixner {
6649bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
6653ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
6663ac7fe5aSThomas Gleixner 	unsigned long flags;
6673ac7fe5aSThomas Gleixner 
6680af462f1SThomas Gleixner 	debug_objects_fill_pool();
66950db04ddSVegard Nossum 
6703ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
6713ac7fe5aSThomas Gleixner 
672aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
6733ac7fe5aSThomas Gleixner 
67463a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
67563a75969SThomas Gleixner 	if (unlikely(!obj)) {
676aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
6773ac7fe5aSThomas Gleixner 		debug_objects_oom();
6783ac7fe5aSThomas Gleixner 		return;
6793ac7fe5aSThomas Gleixner 	}
6803ac7fe5aSThomas Gleixner 
6813ac7fe5aSThomas Gleixner 	switch (obj->state) {
6823ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
6833ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
6843ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
6853ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_INIT;
686aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
687d5f34153SWaiman Long 		return;
6883ac7fe5aSThomas Gleixner 	default:
6893ac7fe5aSThomas Gleixner 		break;
6903ac7fe5aSThomas Gleixner 	}
6913ac7fe5aSThomas Gleixner 
6929bb63626SAndrzej Hajda 	o = *obj;
693aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
6949bb63626SAndrzej Hajda 	debug_print_object(&o, "init");
6959bb63626SAndrzej Hajda 
6969bb63626SAndrzej Hajda 	if (o.state == ODEBUG_STATE_ACTIVE)
6979bb63626SAndrzej Hajda 		debug_object_fixup(descr->fixup_init, addr, o.state);
6983ac7fe5aSThomas Gleixner }
6993ac7fe5aSThomas Gleixner 
7003ac7fe5aSThomas Gleixner /**
7013ac7fe5aSThomas Gleixner  * debug_object_init - debug checks when an object is initialized
7023ac7fe5aSThomas Gleixner  * @addr:	address of the object
7033ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7043ac7fe5aSThomas Gleixner  */
705aedcade6SStephen Boyd void debug_object_init(void *addr, const struct debug_obj_descr *descr)
7063ac7fe5aSThomas Gleixner {
7073ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7083ac7fe5aSThomas Gleixner 		return;
7093ac7fe5aSThomas Gleixner 
7103ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 0);
7113ac7fe5aSThomas Gleixner }
712f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init);
7133ac7fe5aSThomas Gleixner 
7143ac7fe5aSThomas Gleixner /**
7153ac7fe5aSThomas Gleixner  * debug_object_init_on_stack - debug checks when an object on stack is
7163ac7fe5aSThomas Gleixner  *				initialized
7173ac7fe5aSThomas Gleixner  * @addr:	address of the object
7183ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7193ac7fe5aSThomas Gleixner  */
720aedcade6SStephen Boyd void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr)
7213ac7fe5aSThomas Gleixner {
7223ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7233ac7fe5aSThomas Gleixner 		return;
7243ac7fe5aSThomas Gleixner 
7253ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 1);
7263ac7fe5aSThomas Gleixner }
727f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
7283ac7fe5aSThomas Gleixner 
7293ac7fe5aSThomas Gleixner /**
7303ac7fe5aSThomas Gleixner  * debug_object_activate - debug checks when an object is activated
7313ac7fe5aSThomas Gleixner  * @addr:	address of the object
7323ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
733b778ae25SPaul E. McKenney  * Returns 0 for success, -EINVAL for check failed.
7343ac7fe5aSThomas Gleixner  */
735aedcade6SStephen Boyd int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
7363ac7fe5aSThomas Gleixner {
73763a75969SThomas Gleixner 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
7383ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
7393ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
7403ac7fe5aSThomas Gleixner 	unsigned long flags;
7413ac7fe5aSThomas Gleixner 
7423ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
743b778ae25SPaul E. McKenney 		return 0;
7443ac7fe5aSThomas Gleixner 
7450af462f1SThomas Gleixner 	debug_objects_fill_pool();
7460af462f1SThomas Gleixner 
7473ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
7483ac7fe5aSThomas Gleixner 
749aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
7503ac7fe5aSThomas Gleixner 
75163a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, false, true);
7529bb63626SAndrzej Hajda 	if (unlikely(!obj)) {
7539bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
7549bb63626SAndrzej Hajda 		debug_objects_oom();
7559bb63626SAndrzej Hajda 		return 0;
7569bb63626SAndrzej Hajda 	} else if (likely(!IS_ERR(obj))) {
7573ac7fe5aSThomas Gleixner 		switch (obj->state) {
7589bb63626SAndrzej Hajda 		case ODEBUG_STATE_ACTIVE:
7599bb63626SAndrzej Hajda 		case ODEBUG_STATE_DESTROYED:
7609bb63626SAndrzej Hajda 			o = *obj;
7619bb63626SAndrzej Hajda 			break;
7623ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
7633ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
7643ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_ACTIVE;
7659bb63626SAndrzej Hajda 			fallthrough;
7663ac7fe5aSThomas Gleixner 		default:
767aef9cb05SThomas Gleixner 			raw_spin_unlock_irqrestore(&db->lock, flags);
768b778ae25SPaul E. McKenney 			return 0;
7693ac7fe5aSThomas Gleixner 		}
7709bb63626SAndrzej Hajda 	}
77163a75969SThomas Gleixner 
7729bb63626SAndrzej Hajda 	raw_spin_unlock_irqrestore(&db->lock, flags);
77363a75969SThomas Gleixner 	debug_print_object(&o, "activate");
7749bb63626SAndrzej Hajda 
7759bb63626SAndrzej Hajda 	switch (o.state) {
7769bb63626SAndrzej Hajda 	case ODEBUG_STATE_ACTIVE:
7779bb63626SAndrzej Hajda 	case ODEBUG_STATE_NOTAVAILABLE:
7789bb63626SAndrzej Hajda 		if (debug_object_fixup(descr->fixup_activate, addr, o.state))
7799bb63626SAndrzej Hajda 			return 0;
7809bb63626SAndrzej Hajda 		fallthrough;
7819bb63626SAndrzej Hajda 	default:
7829bb63626SAndrzej Hajda 		return -EINVAL;
7839bb63626SAndrzej Hajda 	}
78463a75969SThomas Gleixner }
785f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_activate);
7863ac7fe5aSThomas Gleixner 
7873ac7fe5aSThomas Gleixner /**
7883ac7fe5aSThomas Gleixner  * debug_object_deactivate - debug checks when an object is deactivated
7893ac7fe5aSThomas Gleixner  * @addr:	address of the object
7903ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7913ac7fe5aSThomas Gleixner  */
792aedcade6SStephen Boyd void debug_object_deactivate(void *addr, const struct debug_obj_descr *descr)
7933ac7fe5aSThomas Gleixner {
7949bb63626SAndrzej Hajda 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
7953ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
7963ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
7973ac7fe5aSThomas Gleixner 	unsigned long flags;
7983ac7fe5aSThomas Gleixner 
7993ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8003ac7fe5aSThomas Gleixner 		return;
8013ac7fe5aSThomas Gleixner 
8023ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8033ac7fe5aSThomas Gleixner 
804aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8053ac7fe5aSThomas Gleixner 
8063ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
8073ac7fe5aSThomas Gleixner 	if (obj) {
8083ac7fe5aSThomas Gleixner 		switch (obj->state) {
8099bb63626SAndrzej Hajda 		case ODEBUG_STATE_DESTROYED:
8109bb63626SAndrzej Hajda 			break;
8113ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
8123ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
8133ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_ACTIVE:
8149bb63626SAndrzej Hajda 			if (obj->astate)
8159bb63626SAndrzej Hajda 				break;
8163ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_INACTIVE;
8179bb63626SAndrzej Hajda 			fallthrough;
8183ac7fe5aSThomas Gleixner 		default:
8199bb63626SAndrzej Hajda 			raw_spin_unlock_irqrestore(&db->lock, flags);
8209bb63626SAndrzej Hajda 			return;
8213ac7fe5aSThomas Gleixner 		}
8229bb63626SAndrzej Hajda 		o = *obj;
823d5f34153SWaiman Long 	}
824d5f34153SWaiman Long 
825d5f34153SWaiman Long 	raw_spin_unlock_irqrestore(&db->lock, flags);
8263ac7fe5aSThomas Gleixner 	debug_print_object(&o, "deactivate");
8273ac7fe5aSThomas Gleixner }
828f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_deactivate);
8293ac7fe5aSThomas Gleixner 
8303ac7fe5aSThomas Gleixner /**
8313ac7fe5aSThomas Gleixner  * debug_object_destroy - debug checks when an object is destroyed
8323ac7fe5aSThomas Gleixner  * @addr:	address of the object
8333ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
8343ac7fe5aSThomas Gleixner  */
835aedcade6SStephen Boyd void debug_object_destroy(void *addr, const struct debug_obj_descr *descr)
8363ac7fe5aSThomas Gleixner {
8379bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
8383ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8393ac7fe5aSThomas Gleixner 	unsigned long flags;
8403ac7fe5aSThomas Gleixner 
8413ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8423ac7fe5aSThomas Gleixner 		return;
8433ac7fe5aSThomas Gleixner 
8443ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8453ac7fe5aSThomas Gleixner 
846aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8473ac7fe5aSThomas Gleixner 
8483ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
8499bb63626SAndrzej Hajda 	if (!obj) {
8509bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
8519bb63626SAndrzej Hajda 		return;
8529bb63626SAndrzej Hajda 	}
8533ac7fe5aSThomas Gleixner 
8543ac7fe5aSThomas Gleixner 	switch (obj->state) {
8559bb63626SAndrzej Hajda 	case ODEBUG_STATE_ACTIVE:
8569bb63626SAndrzej Hajda 	case ODEBUG_STATE_DESTROYED:
8579bb63626SAndrzej Hajda 		break;
8583ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
8593ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
8603ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
8613ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_DESTROYED;
8629bb63626SAndrzej Hajda 		fallthrough;
8633ac7fe5aSThomas Gleixner 	default:
864aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
8659bb63626SAndrzej Hajda 		return;
8669bb63626SAndrzej Hajda 	}
8679bb63626SAndrzej Hajda 
8689bb63626SAndrzej Hajda 	o = *obj;
8699bb63626SAndrzej Hajda 	raw_spin_unlock_irqrestore(&db->lock, flags);
8709bb63626SAndrzej Hajda 	debug_print_object(&o, "destroy");
8719bb63626SAndrzej Hajda 
8729bb63626SAndrzej Hajda 	if (o.state == ODEBUG_STATE_ACTIVE)
8739bb63626SAndrzej Hajda 		debug_object_fixup(descr->fixup_destroy, addr, o.state);
8743ac7fe5aSThomas Gleixner }
875f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_destroy);
8763ac7fe5aSThomas Gleixner 
8773ac7fe5aSThomas Gleixner /**
8783ac7fe5aSThomas Gleixner  * debug_object_free - debug checks when an object is freed
8793ac7fe5aSThomas Gleixner  * @addr:	address of the object
8803ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
8813ac7fe5aSThomas Gleixner  */
882aedcade6SStephen Boyd void debug_object_free(void *addr, const struct debug_obj_descr *descr)
8833ac7fe5aSThomas Gleixner {
8849bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
8853ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8863ac7fe5aSThomas Gleixner 	unsigned long flags;
8873ac7fe5aSThomas Gleixner 
8883ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8893ac7fe5aSThomas Gleixner 		return;
8903ac7fe5aSThomas Gleixner 
8913ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8923ac7fe5aSThomas Gleixner 
893aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8943ac7fe5aSThomas Gleixner 
8953ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
8969bb63626SAndrzej Hajda 	if (!obj) {
8979bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
8989bb63626SAndrzej Hajda 		return;
8999bb63626SAndrzej Hajda 	}
9003ac7fe5aSThomas Gleixner 
9013ac7fe5aSThomas Gleixner 	switch (obj->state) {
9023ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
9039bb63626SAndrzej Hajda 		break;
9043ac7fe5aSThomas Gleixner 	default:
9053ac7fe5aSThomas Gleixner 		hlist_del(&obj->node);
906aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
9073ac7fe5aSThomas Gleixner 		free_object(obj);
908673d62ccSVegard Nossum 		return;
9093ac7fe5aSThomas Gleixner 	}
9109bb63626SAndrzej Hajda 
9119bb63626SAndrzej Hajda 	o = *obj;
912aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
9139bb63626SAndrzej Hajda 	debug_print_object(&o, "free");
9149bb63626SAndrzej Hajda 
9159bb63626SAndrzej Hajda 	debug_object_fixup(descr->fixup_free, addr, o.state);
9163ac7fe5aSThomas Gleixner }
917f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_free);
9183ac7fe5aSThomas Gleixner 
919a5d8e467SMathieu Desnoyers /**
920b84d435cSChristine Chan  * debug_object_assert_init - debug checks when object should be init-ed
921b84d435cSChristine Chan  * @addr:	address of the object
922b84d435cSChristine Chan  * @descr:	pointer to an object specific debug description structure
923b84d435cSChristine Chan  */
924aedcade6SStephen Boyd void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
925b84d435cSChristine Chan {
92663a75969SThomas Gleixner 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
927b84d435cSChristine Chan 	struct debug_bucket *db;
928b84d435cSChristine Chan 	struct debug_obj *obj;
929b84d435cSChristine Chan 	unsigned long flags;
930b84d435cSChristine Chan 
931b84d435cSChristine Chan 	if (!debug_objects_enabled)
932b84d435cSChristine Chan 		return;
933b84d435cSChristine Chan 
9340af462f1SThomas Gleixner 	debug_objects_fill_pool();
9350af462f1SThomas Gleixner 
936b84d435cSChristine Chan 	db = get_bucket((unsigned long) addr);
937b84d435cSChristine Chan 
938b84d435cSChristine Chan 	raw_spin_lock_irqsave(&db->lock, flags);
93963a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, false, true);
940b84d435cSChristine Chan 	raw_spin_unlock_irqrestore(&db->lock, flags);
94163a75969SThomas Gleixner 	if (likely(!IS_ERR_OR_NULL(obj)))
94263a75969SThomas Gleixner 		return;
94363a75969SThomas Gleixner 
94463a75969SThomas Gleixner 	/* If NULL the allocation has hit OOM */
94563a75969SThomas Gleixner 	if (!obj) {
94663a75969SThomas Gleixner 		debug_objects_oom();
947b84d435cSChristine Chan 		return;
948b84d435cSChristine Chan 	}
949b84d435cSChristine Chan 
95063a75969SThomas Gleixner 	/* Object is neither tracked nor static. It's not initialized. */
95163a75969SThomas Gleixner 	debug_print_object(&o, "assert_init");
95263a75969SThomas Gleixner 	debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
953b84d435cSChristine Chan }
954f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_assert_init);
955b84d435cSChristine Chan 
956b84d435cSChristine Chan /**
957a5d8e467SMathieu Desnoyers  * debug_object_active_state - debug checks object usage state machine
958a5d8e467SMathieu Desnoyers  * @addr:	address of the object
959a5d8e467SMathieu Desnoyers  * @descr:	pointer to an object specific debug description structure
960a5d8e467SMathieu Desnoyers  * @expect:	expected state
961a5d8e467SMathieu Desnoyers  * @next:	state to move to if expected state is found
962a5d8e467SMathieu Desnoyers  */
963a5d8e467SMathieu Desnoyers void
964aedcade6SStephen Boyd debug_object_active_state(void *addr, const struct debug_obj_descr *descr,
965a5d8e467SMathieu Desnoyers 			  unsigned int expect, unsigned int next)
966a5d8e467SMathieu Desnoyers {
9679bb63626SAndrzej Hajda 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
968a5d8e467SMathieu Desnoyers 	struct debug_bucket *db;
969a5d8e467SMathieu Desnoyers 	struct debug_obj *obj;
970a5d8e467SMathieu Desnoyers 	unsigned long flags;
971a5d8e467SMathieu Desnoyers 
972a5d8e467SMathieu Desnoyers 	if (!debug_objects_enabled)
973a5d8e467SMathieu Desnoyers 		return;
974a5d8e467SMathieu Desnoyers 
975a5d8e467SMathieu Desnoyers 	db = get_bucket((unsigned long) addr);
976a5d8e467SMathieu Desnoyers 
977a5d8e467SMathieu Desnoyers 	raw_spin_lock_irqsave(&db->lock, flags);
978a5d8e467SMathieu Desnoyers 
979a5d8e467SMathieu Desnoyers 	obj = lookup_object(addr, db);
980a5d8e467SMathieu Desnoyers 	if (obj) {
981a5d8e467SMathieu Desnoyers 		switch (obj->state) {
982a5d8e467SMathieu Desnoyers 		case ODEBUG_STATE_ACTIVE:
9839bb63626SAndrzej Hajda 			if (obj->astate != expect)
984a5d8e467SMathieu Desnoyers 				break;
9859bb63626SAndrzej Hajda 			obj->astate = next;
9869bb63626SAndrzej Hajda 			raw_spin_unlock_irqrestore(&db->lock, flags);
9879bb63626SAndrzej Hajda 			return;
988a5d8e467SMathieu Desnoyers 		default:
989a5d8e467SMathieu Desnoyers 			break;
990a5d8e467SMathieu Desnoyers 		}
9919bb63626SAndrzej Hajda 		o = *obj;
992d5f34153SWaiman Long 	}
993d5f34153SWaiman Long 
994d5f34153SWaiman Long 	raw_spin_unlock_irqrestore(&db->lock, flags);
995a5d8e467SMathieu Desnoyers 	debug_print_object(&o, "active_state");
996a5d8e467SMathieu Desnoyers }
997f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_active_state);
998a5d8e467SMathieu Desnoyers 
9993ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
10003ac7fe5aSThomas Gleixner static void __debug_check_no_obj_freed(const void *address, unsigned long size)
10013ac7fe5aSThomas Gleixner {
10023ac7fe5aSThomas Gleixner 	unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
10039bb63626SAndrzej Hajda 	int cnt, objs_checked = 0;
10049bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
10053ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
10061ea9b98bSYang Shi 	struct hlist_node *tmp;
10073ac7fe5aSThomas Gleixner 
10083ac7fe5aSThomas Gleixner 	saddr = (unsigned long) address;
10093ac7fe5aSThomas Gleixner 	eaddr = saddr + size;
10103ac7fe5aSThomas Gleixner 	paddr = saddr & ODEBUG_CHUNK_MASK;
10113ac7fe5aSThomas Gleixner 	chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
10123ac7fe5aSThomas Gleixner 	chunks >>= ODEBUG_CHUNK_SHIFT;
10133ac7fe5aSThomas Gleixner 
10143ac7fe5aSThomas Gleixner 	for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
10153ac7fe5aSThomas Gleixner 		db = get_bucket(paddr);
10163ac7fe5aSThomas Gleixner 
10173ac7fe5aSThomas Gleixner repeat:
10183ac7fe5aSThomas Gleixner 		cnt = 0;
1019aef9cb05SThomas Gleixner 		raw_spin_lock_irqsave(&db->lock, flags);
1020b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
10213ac7fe5aSThomas Gleixner 			cnt++;
10223ac7fe5aSThomas Gleixner 			oaddr = (unsigned long) obj->object;
10233ac7fe5aSThomas Gleixner 			if (oaddr < saddr || oaddr >= eaddr)
10243ac7fe5aSThomas Gleixner 				continue;
10253ac7fe5aSThomas Gleixner 
10263ac7fe5aSThomas Gleixner 			switch (obj->state) {
10273ac7fe5aSThomas Gleixner 			case ODEBUG_STATE_ACTIVE:
10289bb63626SAndrzej Hajda 				o = *obj;
1029aef9cb05SThomas Gleixner 				raw_spin_unlock_irqrestore(&db->lock, flags);
10309bb63626SAndrzej Hajda 				debug_print_object(&o, "free");
10319bb63626SAndrzej Hajda 				debug_object_fixup(o.descr->fixup_free, (void *)oaddr, o.state);
10323ac7fe5aSThomas Gleixner 				goto repeat;
10333ac7fe5aSThomas Gleixner 			default:
10343ac7fe5aSThomas Gleixner 				hlist_del(&obj->node);
1035a7344a68SWaiman Long 				__free_object(obj);
10363ac7fe5aSThomas Gleixner 				break;
10373ac7fe5aSThomas Gleixner 			}
10383ac7fe5aSThomas Gleixner 		}
1039aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
1040673d62ccSVegard Nossum 
10413ac7fe5aSThomas Gleixner 		if (cnt > debug_objects_maxchain)
10423ac7fe5aSThomas Gleixner 			debug_objects_maxchain = cnt;
1043bd9dcd04SYang Shi 
1044bd9dcd04SYang Shi 		objs_checked += cnt;
10453ac7fe5aSThomas Gleixner 	}
1046bd9dcd04SYang Shi 
1047bd9dcd04SYang Shi 	if (objs_checked > debug_objects_maxchecked)
1048bd9dcd04SYang Shi 		debug_objects_maxchecked = objs_checked;
10491ea9b98bSYang Shi 
10501ea9b98bSYang Shi 	/* Schedule work to actually kmem_cache_free() objects */
1051e18328ffSThomas Gleixner 	if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
1052a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
1053a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
1054a7344a68SWaiman Long 	}
10553ac7fe5aSThomas Gleixner }
10563ac7fe5aSThomas Gleixner 
10573ac7fe5aSThomas Gleixner void debug_check_no_obj_freed(const void *address, unsigned long size)
10583ac7fe5aSThomas Gleixner {
10593ac7fe5aSThomas Gleixner 	if (debug_objects_enabled)
10603ac7fe5aSThomas Gleixner 		__debug_check_no_obj_freed(address, size);
10613ac7fe5aSThomas Gleixner }
10623ac7fe5aSThomas Gleixner #endif
10633ac7fe5aSThomas Gleixner 
10643ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_FS
10653ac7fe5aSThomas Gleixner 
10663ac7fe5aSThomas Gleixner static int debug_stats_show(struct seq_file *m, void *v)
10673ac7fe5aSThomas Gleixner {
1068d86998b1SWaiman Long 	int cpu, obj_percpu_free = 0;
1069d86998b1SWaiman Long 
1070d86998b1SWaiman Long 	for_each_possible_cpu(cpu)
107118b8afcbSThomas Gleixner 		obj_percpu_free += per_cpu(pool_pcpu.cnt, cpu);
1072d86998b1SWaiman Long 
10733ac7fe5aSThomas Gleixner 	seq_printf(m, "max_chain     :%d\n", debug_objects_maxchain);
1074bd9dcd04SYang Shi 	seq_printf(m, "max_checked   :%d\n", debug_objects_maxchecked);
10753ac7fe5aSThomas Gleixner 	seq_printf(m, "warnings      :%d\n", debug_objects_warnings);
10763ac7fe5aSThomas Gleixner 	seq_printf(m, "fixups        :%d\n", debug_objects_fixups);
1077e18328ffSThomas Gleixner 	seq_printf(m, "pool_free     :%d\n", pool_count(&pool_global) + obj_percpu_free);
1078d86998b1SWaiman Long 	seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
10793ac7fe5aSThomas Gleixner 	seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
1080d86998b1SWaiman Long 	seq_printf(m, "pool_used     :%d\n", obj_pool_used - obj_percpu_free);
10813ac7fe5aSThomas Gleixner 	seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
1082e18328ffSThomas Gleixner 	seq_printf(m, "on_free_list  :%d\n", pool_count(&pool_to_free));
10830cad93c3SWaiman Long 	seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
10840cad93c3SWaiman Long 	seq_printf(m, "objs_freed    :%d\n", debug_objects_freed);
10853ac7fe5aSThomas Gleixner 	return 0;
10863ac7fe5aSThomas Gleixner }
10870f85c480SQinglang Miao DEFINE_SHOW_ATTRIBUTE(debug_stats);
10883ac7fe5aSThomas Gleixner 
10893ac7fe5aSThomas Gleixner static int __init debug_objects_init_debugfs(void)
10903ac7fe5aSThomas Gleixner {
1091fecb0d95SGreg Kroah-Hartman 	struct dentry *dbgdir;
10923ac7fe5aSThomas Gleixner 
10933ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
10943ac7fe5aSThomas Gleixner 		return 0;
10953ac7fe5aSThomas Gleixner 
10963ac7fe5aSThomas Gleixner 	dbgdir = debugfs_create_dir("debug_objects", NULL);
10973ac7fe5aSThomas Gleixner 
1098fecb0d95SGreg Kroah-Hartman 	debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
10993ac7fe5aSThomas Gleixner 
11003ac7fe5aSThomas Gleixner 	return 0;
11013ac7fe5aSThomas Gleixner }
11023ac7fe5aSThomas Gleixner __initcall(debug_objects_init_debugfs);
11033ac7fe5aSThomas Gleixner 
11043ac7fe5aSThomas Gleixner #else
11053ac7fe5aSThomas Gleixner static inline void debug_objects_init_debugfs(void) { }
11063ac7fe5aSThomas Gleixner #endif
11073ac7fe5aSThomas Gleixner 
11083ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
11093ac7fe5aSThomas Gleixner 
11103ac7fe5aSThomas Gleixner /* Random data structure for the self test */
11113ac7fe5aSThomas Gleixner struct self_test {
11123ac7fe5aSThomas Gleixner 	unsigned long	dummy1[6];
11133ac7fe5aSThomas Gleixner 	int		static_init;
11143ac7fe5aSThomas Gleixner 	unsigned long	dummy2[3];
11153ac7fe5aSThomas Gleixner };
11163ac7fe5aSThomas Gleixner 
1117aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test;
11183ac7fe5aSThomas Gleixner 
1119b9fdac7fSDu, Changbin static bool __init is_static_object(void *addr)
1120b9fdac7fSDu, Changbin {
1121b9fdac7fSDu, Changbin 	struct self_test *obj = addr;
1122b9fdac7fSDu, Changbin 
1123b9fdac7fSDu, Changbin 	return obj->static_init;
1124b9fdac7fSDu, Changbin }
1125b9fdac7fSDu, Changbin 
11263ac7fe5aSThomas Gleixner /*
11273ac7fe5aSThomas Gleixner  * fixup_init is called when:
11283ac7fe5aSThomas Gleixner  * - an active object is initialized
11293ac7fe5aSThomas Gleixner  */
1130b1e4d9d8SDu, Changbin static bool __init fixup_init(void *addr, enum debug_obj_state state)
11313ac7fe5aSThomas Gleixner {
11323ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11333ac7fe5aSThomas Gleixner 
11343ac7fe5aSThomas Gleixner 	switch (state) {
11353ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11363ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11373ac7fe5aSThomas Gleixner 		debug_object_init(obj, &descr_type_test);
1138b1e4d9d8SDu, Changbin 		return true;
11393ac7fe5aSThomas Gleixner 	default:
1140b1e4d9d8SDu, Changbin 		return false;
11413ac7fe5aSThomas Gleixner 	}
11423ac7fe5aSThomas Gleixner }
11433ac7fe5aSThomas Gleixner 
11443ac7fe5aSThomas Gleixner /*
11453ac7fe5aSThomas Gleixner  * fixup_activate is called when:
11463ac7fe5aSThomas Gleixner  * - an active object is activated
1147b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
11483ac7fe5aSThomas Gleixner  */
1149b1e4d9d8SDu, Changbin static bool __init fixup_activate(void *addr, enum debug_obj_state state)
11503ac7fe5aSThomas Gleixner {
11513ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11523ac7fe5aSThomas Gleixner 
11533ac7fe5aSThomas Gleixner 	switch (state) {
11543ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
1155b1e4d9d8SDu, Changbin 		return true;
11563ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11573ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11583ac7fe5aSThomas Gleixner 		debug_object_activate(obj, &descr_type_test);
1159b1e4d9d8SDu, Changbin 		return true;
11603ac7fe5aSThomas Gleixner 
11613ac7fe5aSThomas Gleixner 	default:
1162b1e4d9d8SDu, Changbin 		return false;
11633ac7fe5aSThomas Gleixner 	}
11643ac7fe5aSThomas Gleixner }
11653ac7fe5aSThomas Gleixner 
11663ac7fe5aSThomas Gleixner /*
11673ac7fe5aSThomas Gleixner  * fixup_destroy is called when:
11683ac7fe5aSThomas Gleixner  * - an active object is destroyed
11693ac7fe5aSThomas Gleixner  */
1170b1e4d9d8SDu, Changbin static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
11713ac7fe5aSThomas Gleixner {
11723ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11733ac7fe5aSThomas Gleixner 
11743ac7fe5aSThomas Gleixner 	switch (state) {
11753ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11763ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11773ac7fe5aSThomas Gleixner 		debug_object_destroy(obj, &descr_type_test);
1178b1e4d9d8SDu, Changbin 		return true;
11793ac7fe5aSThomas Gleixner 	default:
1180b1e4d9d8SDu, Changbin 		return false;
11813ac7fe5aSThomas Gleixner 	}
11823ac7fe5aSThomas Gleixner }
11833ac7fe5aSThomas Gleixner 
11843ac7fe5aSThomas Gleixner /*
11853ac7fe5aSThomas Gleixner  * fixup_free is called when:
11863ac7fe5aSThomas Gleixner  * - an active object is freed
11873ac7fe5aSThomas Gleixner  */
1188b1e4d9d8SDu, Changbin static bool __init fixup_free(void *addr, enum debug_obj_state state)
11893ac7fe5aSThomas Gleixner {
11903ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11913ac7fe5aSThomas Gleixner 
11923ac7fe5aSThomas Gleixner 	switch (state) {
11933ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11943ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11953ac7fe5aSThomas Gleixner 		debug_object_free(obj, &descr_type_test);
1196b1e4d9d8SDu, Changbin 		return true;
11973ac7fe5aSThomas Gleixner 	default:
1198b1e4d9d8SDu, Changbin 		return false;
11993ac7fe5aSThomas Gleixner 	}
12003ac7fe5aSThomas Gleixner }
12013ac7fe5aSThomas Gleixner 
12021fb2f77cSHenrik Kretzschmar static int __init
12033ac7fe5aSThomas Gleixner check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
12043ac7fe5aSThomas Gleixner {
12053ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
12063ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
12073ac7fe5aSThomas Gleixner 	unsigned long flags;
12083ac7fe5aSThomas Gleixner 	int res = -EINVAL;
12093ac7fe5aSThomas Gleixner 
12103ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
12113ac7fe5aSThomas Gleixner 
1212aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
12133ac7fe5aSThomas Gleixner 
12143ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
12153ac7fe5aSThomas Gleixner 	if (!obj && state != ODEBUG_STATE_NONE) {
12165cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
12173ac7fe5aSThomas Gleixner 		goto out;
12183ac7fe5aSThomas Gleixner 	}
12193ac7fe5aSThomas Gleixner 	if (obj && obj->state != state) {
12205cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
12213ac7fe5aSThomas Gleixner 		       obj->state, state);
12223ac7fe5aSThomas Gleixner 		goto out;
12233ac7fe5aSThomas Gleixner 	}
12243ac7fe5aSThomas Gleixner 	if (fixups != debug_objects_fixups) {
12255cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
12263ac7fe5aSThomas Gleixner 		       fixups, debug_objects_fixups);
12273ac7fe5aSThomas Gleixner 		goto out;
12283ac7fe5aSThomas Gleixner 	}
12293ac7fe5aSThomas Gleixner 	if (warnings != debug_objects_warnings) {
12305cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
12313ac7fe5aSThomas Gleixner 		       warnings, debug_objects_warnings);
12323ac7fe5aSThomas Gleixner 		goto out;
12333ac7fe5aSThomas Gleixner 	}
12343ac7fe5aSThomas Gleixner 	res = 0;
12353ac7fe5aSThomas Gleixner out:
1236aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
12373ac7fe5aSThomas Gleixner 	if (res)
1238661cc28bSThomas Gleixner 		debug_objects_enabled = false;
12393ac7fe5aSThomas Gleixner 	return res;
12403ac7fe5aSThomas Gleixner }
12413ac7fe5aSThomas Gleixner 
1242aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test = {
12433ac7fe5aSThomas Gleixner 	.name			= "selftest",
1244b9fdac7fSDu, Changbin 	.is_static_object	= is_static_object,
12453ac7fe5aSThomas Gleixner 	.fixup_init		= fixup_init,
12463ac7fe5aSThomas Gleixner 	.fixup_activate		= fixup_activate,
12473ac7fe5aSThomas Gleixner 	.fixup_destroy		= fixup_destroy,
12483ac7fe5aSThomas Gleixner 	.fixup_free		= fixup_free,
12493ac7fe5aSThomas Gleixner };
12503ac7fe5aSThomas Gleixner 
12513ac7fe5aSThomas Gleixner static __initdata struct self_test obj = { .static_init = 0 };
12523ac7fe5aSThomas Gleixner 
125355fb412eSThomas Gleixner static bool __init debug_objects_selftest(void)
12543ac7fe5aSThomas Gleixner {
12553ac7fe5aSThomas Gleixner 	int fixups, oldfixups, warnings, oldwarnings;
12563ac7fe5aSThomas Gleixner 	unsigned long flags;
12573ac7fe5aSThomas Gleixner 
12583ac7fe5aSThomas Gleixner 	local_irq_save(flags);
12593ac7fe5aSThomas Gleixner 
12603ac7fe5aSThomas Gleixner 	fixups = oldfixups = debug_objects_fixups;
12613ac7fe5aSThomas Gleixner 	warnings = oldwarnings = debug_objects_warnings;
12623ac7fe5aSThomas Gleixner 	descr_test = &descr_type_test;
12633ac7fe5aSThomas Gleixner 
12643ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12653ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
12663ac7fe5aSThomas Gleixner 		goto out;
12673ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12683ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
12693ac7fe5aSThomas Gleixner 		goto out;
12703ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12713ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
12723ac7fe5aSThomas Gleixner 		goto out;
12733ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
12743ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
12753ac7fe5aSThomas Gleixner 		goto out;
12763ac7fe5aSThomas Gleixner 	debug_object_destroy(&obj, &descr_type_test);
12773ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
12783ac7fe5aSThomas Gleixner 		goto out;
12793ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12803ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12813ac7fe5aSThomas Gleixner 		goto out;
12823ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12833ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12843ac7fe5aSThomas Gleixner 		goto out;
12853ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
12863ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12873ac7fe5aSThomas Gleixner 		goto out;
12883ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
12893ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
12903ac7fe5aSThomas Gleixner 		goto out;
12913ac7fe5aSThomas Gleixner 
12923ac7fe5aSThomas Gleixner 	obj.static_init = 1;
12933ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12949f78ff00SStephen Boyd 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
12953ac7fe5aSThomas Gleixner 		goto out;
12963ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12973ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
12983ac7fe5aSThomas Gleixner 		goto out;
12993ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
13003ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
13013ac7fe5aSThomas Gleixner 		goto out;
13023ac7fe5aSThomas Gleixner 
13033ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
13043ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
13053ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
13063ac7fe5aSThomas Gleixner 		goto out;
13073ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13083ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
13093ac7fe5aSThomas Gleixner 		goto out;
13103ac7fe5aSThomas Gleixner 	__debug_check_no_obj_freed(&obj, sizeof(obj));
13113ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
13123ac7fe5aSThomas Gleixner 		goto out;
13133ac7fe5aSThomas Gleixner #endif
1314719e4843SFabian Frederick 	pr_info("selftest passed\n");
13153ac7fe5aSThomas Gleixner 
13163ac7fe5aSThomas Gleixner out:
13173ac7fe5aSThomas Gleixner 	debug_objects_fixups = oldfixups;
13183ac7fe5aSThomas Gleixner 	debug_objects_warnings = oldwarnings;
13193ac7fe5aSThomas Gleixner 	descr_test = NULL;
13203ac7fe5aSThomas Gleixner 
13213ac7fe5aSThomas Gleixner 	local_irq_restore(flags);
1322661cc28bSThomas Gleixner 	return debug_objects_enabled;
13233ac7fe5aSThomas Gleixner }
13243ac7fe5aSThomas Gleixner #else
132555fb412eSThomas Gleixner static inline bool debug_objects_selftest(void) { return true; }
13263ac7fe5aSThomas Gleixner #endif
13273ac7fe5aSThomas Gleixner 
13283ac7fe5aSThomas Gleixner /*
13293ac7fe5aSThomas Gleixner  * Called during early boot to initialize the hash buckets and link
13303ac7fe5aSThomas Gleixner  * the static object pool objects into the poll list. After this call
13313ac7fe5aSThomas Gleixner  * the object tracker is fully operational.
13323ac7fe5aSThomas Gleixner  */
13333ac7fe5aSThomas Gleixner void __init debug_objects_early_init(void)
13343ac7fe5aSThomas Gleixner {
13353ac7fe5aSThomas Gleixner 	int i;
13363ac7fe5aSThomas Gleixner 
13373ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1338aef9cb05SThomas Gleixner 		raw_spin_lock_init(&obj_hash[i].lock);
13393ac7fe5aSThomas Gleixner 
1340cb58d190SThomas Gleixner 	/* Keep early boot simple and add everything to the boot list */
13413ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1342cb58d190SThomas Gleixner 		hlist_add_head(&obj_static_pool[i].node, &pool_boot);
13433ac7fe5aSThomas Gleixner }
13443ac7fe5aSThomas Gleixner 
13453ac7fe5aSThomas Gleixner /*
134655fb412eSThomas Gleixner  * Convert the statically allocated objects to dynamic ones.
134755fb412eSThomas Gleixner  * debug_objects_mem_init() is called early so only one CPU is up and
134855fb412eSThomas Gleixner  * interrupts are disabled, which means it is safe to replace the active
134955fb412eSThomas Gleixner  * object references.
13501be1cb7bSThomas Gleixner  */
135155fb412eSThomas Gleixner static bool __init debug_objects_replace_static_objects(struct kmem_cache *cache)
13521be1cb7bSThomas Gleixner {
13531be1cb7bSThomas Gleixner 	struct debug_bucket *db = obj_hash;
13541be1cb7bSThomas Gleixner 	struct debug_obj *obj, *new;
135555fb412eSThomas Gleixner 	struct hlist_node *tmp;
13561be1cb7bSThomas Gleixner 	HLIST_HEAD(objects);
1357241463f4SThomas Gleixner 	int i;
13581be1cb7bSThomas Gleixner 
13591be1cb7bSThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
136055fb412eSThomas Gleixner 		obj = kmem_cache_zalloc(cache, GFP_KERNEL);
13611be1cb7bSThomas Gleixner 		if (!obj)
13621be1cb7bSThomas Gleixner 			goto free;
13631be1cb7bSThomas Gleixner 		hlist_add_head(&obj->node, &objects);
13641be1cb7bSThomas Gleixner 	}
13651be1cb7bSThomas Gleixner 
1366e18328ffSThomas Gleixner 	debug_objects_allocated = ODEBUG_POOL_SIZE;
1367e18328ffSThomas Gleixner 	pool_global.cnt = ODEBUG_POOL_SIZE;
1368eabb7f1aSwuchi 
13691be1cb7bSThomas Gleixner 	/*
1370cb58d190SThomas Gleixner 	 * Move the allocated objects to the global pool and disconnect the
1371cb58d190SThomas Gleixner 	 * boot pool.
1372a0ae9504SZhen Lei 	 */
1373e18328ffSThomas Gleixner 	hlist_move_list(&objects, &pool_global.objects);
1374cb58d190SThomas Gleixner 	pool_boot.first = NULL;
13751be1cb7bSThomas Gleixner 
13761be1cb7bSThomas Gleixner 	/* Replace the active object references */
13771be1cb7bSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
13781be1cb7bSThomas Gleixner 		hlist_move_list(&db->list, &objects);
13791be1cb7bSThomas Gleixner 
1380b67bfe0dSSasha Levin 		hlist_for_each_entry(obj, &objects, node) {
1381e18328ffSThomas Gleixner 			new = hlist_entry(pool_global.objects.first, typeof(*obj), node);
13821be1cb7bSThomas Gleixner 			hlist_del(&new->node);
1383e18328ffSThomas Gleixner 			pool_global.cnt--;
13841be1cb7bSThomas Gleixner 			/* copy object data */
13851be1cb7bSThomas Gleixner 			*new = *obj;
13861be1cb7bSThomas Gleixner 			hlist_add_head(&new->node, &db->list);
13871be1cb7bSThomas Gleixner 		}
13881be1cb7bSThomas Gleixner 	}
138955fb412eSThomas Gleixner 	return true;
13901be1cb7bSThomas Gleixner free:
139149a5cb82SThomas Gleixner 	/* Can't use free_object_list() as the cache is not populated yet */
1392b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(obj, tmp, &objects, node) {
13931be1cb7bSThomas Gleixner 		hlist_del(&obj->node);
139455fb412eSThomas Gleixner 		kmem_cache_free(cache, obj);
13951be1cb7bSThomas Gleixner 	}
139655fb412eSThomas Gleixner 	return false;
13971be1cb7bSThomas Gleixner }
13981be1cb7bSThomas Gleixner 
13991be1cb7bSThomas Gleixner /*
14003ac7fe5aSThomas Gleixner  * Called after the kmem_caches are functional to setup a dedicated
14013ac7fe5aSThomas Gleixner  * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
14023ac7fe5aSThomas Gleixner  * prevents that the debug code is called on kmem_cache_free() for the
14033ac7fe5aSThomas Gleixner  * debug tracker objects to avoid recursive calls.
14043ac7fe5aSThomas Gleixner  */
14053ac7fe5aSThomas Gleixner void __init debug_objects_mem_init(void)
14063ac7fe5aSThomas Gleixner {
140755fb412eSThomas Gleixner 	struct kmem_cache *cache;
14083f397bf9SThomas Gleixner 	int extras;
1409d86998b1SWaiman Long 
14103ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
14113ac7fe5aSThomas Gleixner 		return;
14123ac7fe5aSThomas Gleixner 
141355fb412eSThomas Gleixner 	if (!debug_objects_selftest())
1414eabb7f1aSwuchi 		return;
141555fb412eSThomas Gleixner 
141655fb412eSThomas Gleixner 	cache = kmem_cache_create("debug_objects_cache", sizeof (struct debug_obj), 0,
141755fb412eSThomas Gleixner 				  SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE, NULL);
141855fb412eSThomas Gleixner 
141955fb412eSThomas Gleixner 	if (!cache || !debug_objects_replace_static_objects(cache)) {
1420661cc28bSThomas Gleixner 		debug_objects_enabled = false;
142155fb412eSThomas Gleixner 		pr_warn("Out of memory.\n");
142255fb412eSThomas Gleixner 		return;
142355fb412eSThomas Gleixner 	}
142455fb412eSThomas Gleixner 
142555fb412eSThomas Gleixner 	/*
142655fb412eSThomas Gleixner 	 * Adjust the thresholds for allocating and freeing objects
142755fb412eSThomas Gleixner 	 * according to the number of possible CPUs available in the
142855fb412eSThomas Gleixner 	 * system.
142955fb412eSThomas Gleixner 	 */
143055fb412eSThomas Gleixner 	extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
143196a9a042SThomas Gleixner 	pool_global.max_cnt += extras;
143296a9a042SThomas Gleixner 	pool_global.min_cnt += extras;
143355fb412eSThomas Gleixner 
143455fb412eSThomas Gleixner 	/* Everything worked. Expose the cache */
143555fb412eSThomas Gleixner 	obj_cache = cache;
1436634d61f4SWaiman Long 
143788451f2cSZqiang #ifdef CONFIG_HOTPLUG_CPU
143888451f2cSZqiang 	cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL,
143988451f2cSZqiang 				  object_cpu_offline);
144088451f2cSZqiang #endif
144155fb412eSThomas Gleixner 	return;
14423ac7fe5aSThomas Gleixner }
1443