xref: /linux-6.15/lib/debugobjects.c (revision 96a9a042)
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;
49*96a9a042SThomas Gleixner 	unsigned int		min_cnt;
50*96a9a042SThomas Gleixner 	unsigned int		max_cnt;
51e18328ffSThomas Gleixner } ____cacheline_aligned;
52e18328ffSThomas Gleixner 
53*96a9a042SThomas Gleixner 
54*96a9a042SThomas Gleixner static DEFINE_PER_CPU_ALIGNED(struct obj_pool, pool_pcpu)  = {
55*96a9a042SThomas Gleixner 	.max_cnt	= ODEBUG_POOL_PERCPU_SIZE,
56*96a9a042SThomas 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 
64*96a9a042SThomas Gleixner static struct obj_pool pool_global = {
65*96a9a042SThomas Gleixner 	.min_cnt	= ODEBUG_POOL_MIN_LEVEL,
66*96a9a042SThomas Gleixner 	.max_cnt	= ODEBUG_POOL_SIZE,
67*96a9a042SThomas Gleixner };
68*96a9a042SThomas Gleixner 
69*96a9a042SThomas Gleixner static struct obj_pool pool_to_free = {
70*96a9a042SThomas Gleixner 	.max_cnt	= UINT_MAX,
71*96a9a042SThomas 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 
134*96a9a042SThomas Gleixner static __always_inline bool pool_should_refill(struct obj_pool *pool)
135e18328ffSThomas Gleixner {
136*96a9a042SThomas Gleixner 	return pool_count(pool) < pool->min_cnt;
137e18328ffSThomas Gleixner }
138e18328ffSThomas Gleixner 
139*96a9a042SThomas Gleixner static __always_inline bool pool_must_refill(struct obj_pool *pool)
140e18328ffSThomas Gleixner {
141*96a9a042SThomas Gleixner 	return pool_count(pool) < pool->min_cnt / 2;
142e18328ffSThomas Gleixner }
143e18328ffSThomas Gleixner 
14449a5cb82SThomas Gleixner static void free_object_list(struct hlist_head *head)
14549a5cb82SThomas Gleixner {
14649a5cb82SThomas Gleixner 	struct hlist_node *tmp;
14749a5cb82SThomas Gleixner 	struct debug_obj *obj;
14849a5cb82SThomas Gleixner 	int cnt = 0;
14949a5cb82SThomas Gleixner 
15049a5cb82SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, head, node) {
15149a5cb82SThomas Gleixner 		hlist_del(&obj->node);
15249a5cb82SThomas Gleixner 		kmem_cache_free(obj_cache, obj);
15349a5cb82SThomas Gleixner 		cnt++;
15449a5cb82SThomas Gleixner 	}
15549a5cb82SThomas Gleixner 	debug_objects_freed += cnt;
15649a5cb82SThomas Gleixner }
15749a5cb82SThomas Gleixner 
158d8c6cd3aSZhen Lei static void fill_pool_from_freelist(void)
1593ac7fe5aSThomas Gleixner {
160d8c6cd3aSZhen Lei 	static unsigned long state;
161d26bf505SWaiman Long 	struct debug_obj *obj;
1623ac7fe5aSThomas Gleixner 
16336c4ead6SYang Shi 	/*
16463a4a9b5SZhen Lei 	 * Reuse objs from the global obj_to_free list; they will be
16563a4a9b5SZhen Lei 	 * reinitialized when allocating.
16636c4ead6SYang Shi 	 */
167e18328ffSThomas Gleixner 	if (!pool_count(&pool_to_free))
168d8c6cd3aSZhen Lei 		return;
169d8c6cd3aSZhen Lei 
170d8c6cd3aSZhen Lei 	/*
171d8c6cd3aSZhen Lei 	 * Prevent the context from being scheduled or interrupted after
172d8c6cd3aSZhen Lei 	 * setting the state flag;
173d8c6cd3aSZhen Lei 	 */
174d8c6cd3aSZhen Lei 	guard(irqsave)();
175d8c6cd3aSZhen Lei 
176d8c6cd3aSZhen Lei 	/*
177d8c6cd3aSZhen Lei 	 * Avoid lock contention on &pool_lock and avoid making the cache
178d8c6cd3aSZhen Lei 	 * line exclusive by testing the bit before attempting to set it.
179d8c6cd3aSZhen Lei 	 */
180d8c6cd3aSZhen Lei 	if (test_bit(0, &state) || test_and_set_bit(0, &state))
181d8c6cd3aSZhen Lei 		return;
182d8c6cd3aSZhen Lei 
183d8c6cd3aSZhen Lei 	guard(raw_spinlock)(&pool_lock);
18436c4ead6SYang Shi 	/*
18536c4ead6SYang Shi 	 * Recheck with the lock held as the worker thread might have
18636c4ead6SYang Shi 	 * won the race and freed the global free list already.
18736c4ead6SYang Shi 	 */
188*96a9a042SThomas Gleixner 	while (pool_to_free.cnt && (pool_global.cnt < pool_global.min_cnt)) {
189e18328ffSThomas Gleixner 		obj = hlist_entry(pool_to_free.objects.first, typeof(*obj), node);
19036c4ead6SYang Shi 		hlist_del(&obj->node);
191e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt - 1);
192e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_global.objects);
193e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + 1);
19436c4ead6SYang Shi 	}
195d8c6cd3aSZhen Lei 	clear_bit(0, &state);
19636c4ead6SYang Shi }
19736c4ead6SYang Shi 
198d8c6cd3aSZhen Lei static void fill_pool(void)
199d8c6cd3aSZhen Lei {
200d8c6cd3aSZhen Lei 	static atomic_t cpus_allocating;
201d8c6cd3aSZhen Lei 
202d8c6cd3aSZhen Lei 	/*
203d8c6cd3aSZhen Lei 	 * Avoid allocation and lock contention when:
204d8c6cd3aSZhen Lei 	 *   - One other CPU is already allocating
205d8c6cd3aSZhen Lei 	 *   - the global pool has not reached the critical level yet
206d8c6cd3aSZhen Lei 	 */
207*96a9a042SThomas Gleixner 	if (!pool_must_refill(&pool_global) && atomic_read(&cpus_allocating))
2081fda107dSThomas Gleixner 		return;
2093ac7fe5aSThomas Gleixner 
210d8c6cd3aSZhen Lei 	atomic_inc(&cpus_allocating);
211*96a9a042SThomas Gleixner 	while (pool_should_refill(&pool_global)) {
212813fd078SZhen Lei 		struct debug_obj *new, *last = NULL;
213813fd078SZhen Lei 		HLIST_HEAD(head);
214d26bf505SWaiman Long 		int cnt;
2153ac7fe5aSThomas Gleixner 
216d26bf505SWaiman Long 		for (cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) {
217d8c6cd3aSZhen Lei 			new = kmem_cache_zalloc(obj_cache, __GFP_HIGH | __GFP_NOWARN);
218813fd078SZhen Lei 			if (!new)
219d26bf505SWaiman Long 				break;
220813fd078SZhen Lei 			hlist_add_head(&new->node, &head);
221813fd078SZhen Lei 			if (!last)
222813fd078SZhen Lei 				last = new;
223d26bf505SWaiman Long 		}
224d26bf505SWaiman Long 		if (!cnt)
225d8c6cd3aSZhen Lei 			break;
2263ac7fe5aSThomas Gleixner 
227d8c6cd3aSZhen Lei 		guard(raw_spinlock_irqsave)(&pool_lock);
228e18328ffSThomas Gleixner 		hlist_splice_init(&head, &last->node, &pool_global.objects);
229813fd078SZhen Lei 		debug_objects_allocated += cnt;
230e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + cnt);
2313ac7fe5aSThomas Gleixner 	}
232d8c6cd3aSZhen Lei 	atomic_dec(&cpus_allocating);
2333ac7fe5aSThomas Gleixner }
2343ac7fe5aSThomas Gleixner 
2353ac7fe5aSThomas Gleixner /*
2363ac7fe5aSThomas Gleixner  * Lookup an object in the hash bucket.
2373ac7fe5aSThomas Gleixner  */
2383ac7fe5aSThomas Gleixner static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
2393ac7fe5aSThomas Gleixner {
2403ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
2413ac7fe5aSThomas Gleixner 	int cnt = 0;
2423ac7fe5aSThomas Gleixner 
243b67bfe0dSSasha Levin 	hlist_for_each_entry(obj, &b->list, node) {
2443ac7fe5aSThomas Gleixner 		cnt++;
2453ac7fe5aSThomas Gleixner 		if (obj->object == addr)
2463ac7fe5aSThomas Gleixner 			return obj;
2473ac7fe5aSThomas Gleixner 	}
2483ac7fe5aSThomas Gleixner 	if (cnt > debug_objects_maxchain)
2493ac7fe5aSThomas Gleixner 		debug_objects_maxchain = cnt;
2503ac7fe5aSThomas Gleixner 
2513ac7fe5aSThomas Gleixner 	return NULL;
2523ac7fe5aSThomas Gleixner }
2533ac7fe5aSThomas Gleixner 
2543ac7fe5aSThomas Gleixner /*
255d86998b1SWaiman Long  * Allocate a new object from the hlist
256d86998b1SWaiman Long  */
257d86998b1SWaiman Long static struct debug_obj *__alloc_object(struct hlist_head *list)
258d86998b1SWaiman Long {
259d86998b1SWaiman Long 	struct debug_obj *obj = NULL;
260d86998b1SWaiman Long 
261d86998b1SWaiman Long 	if (list->first) {
262d86998b1SWaiman Long 		obj = hlist_entry(list->first, typeof(*obj), node);
263d86998b1SWaiman Long 		hlist_del(&obj->node);
264d86998b1SWaiman Long 	}
265d86998b1SWaiman Long 
266d86998b1SWaiman Long 	return obj;
267d86998b1SWaiman Long }
268d86998b1SWaiman Long 
2693ac7fe5aSThomas Gleixner static struct debug_obj *
270aedcade6SStephen Boyd alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *descr)
2713ac7fe5aSThomas Gleixner {
27218b8afcbSThomas Gleixner 	struct obj_pool *percpu_pool = this_cpu_ptr(&pool_pcpu);
273d86998b1SWaiman Long 	struct debug_obj *obj;
274d86998b1SWaiman Long 
275d86998b1SWaiman Long 	if (likely(obj_cache)) {
27618b8afcbSThomas Gleixner 		obj = __alloc_object(&percpu_pool->objects);
277d86998b1SWaiman Long 		if (obj) {
27818b8afcbSThomas Gleixner 			percpu_pool->cnt--;
279d86998b1SWaiman Long 			goto init_obj;
280d86998b1SWaiman Long 		}
281cb58d190SThomas Gleixner 	} else {
282cb58d190SThomas Gleixner 		obj = __alloc_object(&pool_boot);
283cb58d190SThomas Gleixner 		goto init_obj;
284d86998b1SWaiman Long 	}
2853ac7fe5aSThomas Gleixner 
286aef9cb05SThomas Gleixner 	raw_spin_lock(&pool_lock);
287e18328ffSThomas Gleixner 	obj = __alloc_object(&pool_global.objects);
288d86998b1SWaiman Long 	if (obj) {
2893ac7fe5aSThomas Gleixner 		obj_pool_used++;
290e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt - 1);
291634d61f4SWaiman Long 
292634d61f4SWaiman Long 		/*
293634d61f4SWaiman Long 		 * Looking ahead, allocate one batch of debug objects and
294634d61f4SWaiman Long 		 * put them into the percpu free pool.
295634d61f4SWaiman Long 		 */
296634d61f4SWaiman Long 		if (likely(obj_cache)) {
297634d61f4SWaiman Long 			int i;
298634d61f4SWaiman Long 
299634d61f4SWaiman Long 			for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
300634d61f4SWaiman Long 				struct debug_obj *obj2;
301634d61f4SWaiman Long 
302e18328ffSThomas Gleixner 				obj2 = __alloc_object(&pool_global.objects);
303634d61f4SWaiman Long 				if (!obj2)
304634d61f4SWaiman Long 					break;
30518b8afcbSThomas Gleixner 				hlist_add_head(&obj2->node, &percpu_pool->objects);
30618b8afcbSThomas Gleixner 				percpu_pool->cnt++;
307634d61f4SWaiman Long 				obj_pool_used++;
308e18328ffSThomas Gleixner 				WRITE_ONCE(pool_global.cnt, pool_global.cnt - 1);
309634d61f4SWaiman Long 			}
310634d61f4SWaiman Long 		}
311634d61f4SWaiman Long 
3123ac7fe5aSThomas Gleixner 		if (obj_pool_used > obj_pool_max_used)
3133ac7fe5aSThomas Gleixner 			obj_pool_max_used = obj_pool_used;
3143ac7fe5aSThomas Gleixner 
315e18328ffSThomas Gleixner 		if (pool_global.cnt < obj_pool_min_free)
316e18328ffSThomas Gleixner 			obj_pool_min_free = pool_global.cnt;
3173ac7fe5aSThomas Gleixner 	}
318aef9cb05SThomas Gleixner 	raw_spin_unlock(&pool_lock);
3193ac7fe5aSThomas Gleixner 
320d86998b1SWaiman Long init_obj:
321d86998b1SWaiman Long 	if (obj) {
322d86998b1SWaiman Long 		obj->object = addr;
323d86998b1SWaiman Long 		obj->descr  = descr;
324d86998b1SWaiman Long 		obj->state  = ODEBUG_STATE_NONE;
325d86998b1SWaiman Long 		obj->astate = 0;
326d86998b1SWaiman Long 		hlist_add_head(&obj->node, &b->list);
327d86998b1SWaiman Long 	}
3283ac7fe5aSThomas Gleixner 	return obj;
3293ac7fe5aSThomas Gleixner }
3303ac7fe5aSThomas Gleixner 
3313ac7fe5aSThomas Gleixner /*
332337fff8bSThomas Gleixner  * workqueue function to free objects.
333858274b6SWaiman Long  *
334858274b6SWaiman Long  * To reduce contention on the global pool_lock, the actual freeing of
335636e1970SYang Shi  * debug objects will be delayed if the pool_lock is busy.
336337fff8bSThomas Gleixner  */
337337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work)
338337fff8bSThomas Gleixner {
33936c4ead6SYang Shi 	struct debug_obj *obj;
340337fff8bSThomas Gleixner 	unsigned long flags;
34136c4ead6SYang Shi 	HLIST_HEAD(tofree);
342337fff8bSThomas Gleixner 
343a7344a68SWaiman Long 	WRITE_ONCE(obj_freeing, false);
344858274b6SWaiman Long 	if (!raw_spin_trylock_irqsave(&pool_lock, flags))
345858274b6SWaiman Long 		return;
34636c4ead6SYang Shi 
347*96a9a042SThomas Gleixner 	if (pool_global.cnt >= pool_global.max_cnt)
348a7344a68SWaiman Long 		goto free_objs;
349a7344a68SWaiman Long 
35036c4ead6SYang Shi 	/*
35136c4ead6SYang Shi 	 * The objs on the pool list might be allocated before the work is
35236c4ead6SYang Shi 	 * run, so recheck if pool list it full or not, if not fill pool
353a7344a68SWaiman Long 	 * list from the global free list. As it is likely that a workload
354a7344a68SWaiman Long 	 * may be gearing up to use more and more objects, don't free any
355a7344a68SWaiman Long 	 * of them until the next round.
35636c4ead6SYang Shi 	 */
357*96a9a042SThomas Gleixner 	while (pool_to_free.cnt && pool_global.cnt < pool_global.max_cnt) {
358e18328ffSThomas Gleixner 		obj = hlist_entry(pool_to_free.objects.first, typeof(*obj), node);
35936c4ead6SYang Shi 		hlist_del(&obj->node);
360e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_global.objects);
361e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt - 1);
362e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + 1);
36336c4ead6SYang Shi 	}
364a7344a68SWaiman Long 	raw_spin_unlock_irqrestore(&pool_lock, flags);
365a7344a68SWaiman Long 	return;
36636c4ead6SYang Shi 
367a7344a68SWaiman Long free_objs:
36836c4ead6SYang Shi 	/*
36936c4ead6SYang Shi 	 * Pool list is already full and there are still objs on the free
37036c4ead6SYang Shi 	 * list. Move remaining free objs to a temporary list to free the
37136c4ead6SYang Shi 	 * memory outside the pool_lock held region.
37236c4ead6SYang Shi 	 */
373e18328ffSThomas Gleixner 	if (pool_to_free.cnt) {
374e18328ffSThomas Gleixner 		hlist_move_list(&pool_to_free.objects, &tofree);
375e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, 0);
37636c4ead6SYang Shi 	}
377aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&pool_lock, flags);
37836c4ead6SYang Shi 
37949a5cb82SThomas Gleixner 	free_object_list(&tofree);
380337fff8bSThomas Gleixner }
381337fff8bSThomas Gleixner 
382a7344a68SWaiman Long static void __free_object(struct debug_obj *obj)
383636e1970SYang Shi {
384634d61f4SWaiman Long 	struct debug_obj *objs[ODEBUG_BATCH_SIZE];
38518b8afcbSThomas Gleixner 	struct obj_pool *percpu_pool;
386634d61f4SWaiman Long 	int lookahead_count = 0;
387636e1970SYang Shi 	bool work;
388636e1970SYang Shi 
389cb58d190SThomas Gleixner 	guard(irqsave)();
390cb58d190SThomas Gleixner 
391cb58d190SThomas Gleixner 	if (unlikely(!obj_cache)) {
392cb58d190SThomas Gleixner 		hlist_add_head(&obj->node, &pool_boot);
393cb58d190SThomas Gleixner 		return;
394cb58d190SThomas Gleixner 	}
395634d61f4SWaiman Long 
396d86998b1SWaiman Long 	/*
397d86998b1SWaiman Long 	 * Try to free it into the percpu pool first.
398d86998b1SWaiman Long 	 */
39918b8afcbSThomas Gleixner 	percpu_pool = this_cpu_ptr(&pool_pcpu);
40018b8afcbSThomas Gleixner 	if (percpu_pool->cnt < ODEBUG_POOL_PERCPU_SIZE) {
40118b8afcbSThomas Gleixner 		hlist_add_head(&obj->node, &percpu_pool->objects);
40218b8afcbSThomas Gleixner 		percpu_pool->cnt++;
403a7344a68SWaiman Long 		return;
404d86998b1SWaiman Long 	}
405d86998b1SWaiman Long 
406634d61f4SWaiman Long 	/*
407634d61f4SWaiman Long 	 * As the percpu pool is full, look ahead and pull out a batch
408634d61f4SWaiman Long 	 * of objects from the percpu pool and free them as well.
409634d61f4SWaiman Long 	 */
410634d61f4SWaiman Long 	for (; lookahead_count < ODEBUG_BATCH_SIZE; lookahead_count++) {
41118b8afcbSThomas Gleixner 		objs[lookahead_count] = __alloc_object(&percpu_pool->objects);
412634d61f4SWaiman Long 		if (!objs[lookahead_count])
413634d61f4SWaiman Long 			break;
41418b8afcbSThomas Gleixner 		percpu_pool->cnt--;
415634d61f4SWaiman Long 	}
416634d61f4SWaiman Long 
417d86998b1SWaiman Long 	raw_spin_lock(&pool_lock);
418*96a9a042SThomas Gleixner 	work = (pool_global.cnt > pool_global.max_cnt) && obj_cache &&
419e18328ffSThomas Gleixner 	       (pool_to_free.cnt < ODEBUG_FREE_WORK_MAX);
420636e1970SYang Shi 	obj_pool_used--;
421636e1970SYang Shi 
422636e1970SYang Shi 	if (work) {
423e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt + 1);
424e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_to_free.objects);
425634d61f4SWaiman Long 		if (lookahead_count) {
426e18328ffSThomas Gleixner 			WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt + lookahead_count);
427634d61f4SWaiman Long 			obj_pool_used -= lookahead_count;
428634d61f4SWaiman Long 			while (lookahead_count) {
429634d61f4SWaiman Long 				hlist_add_head(&objs[--lookahead_count]->node,
430e18328ffSThomas Gleixner 					       &pool_to_free.objects);
431634d61f4SWaiman Long 			}
432634d61f4SWaiman Long 		}
433a7344a68SWaiman Long 
434*96a9a042SThomas Gleixner 		if ((pool_global.cnt > pool_global.max_cnt) &&
435e18328ffSThomas Gleixner 		    (pool_to_free.cnt < ODEBUG_FREE_WORK_MAX)) {
436a7344a68SWaiman Long 			int i;
437a7344a68SWaiman Long 
438a7344a68SWaiman Long 			/*
439a7344a68SWaiman Long 			 * Free one more batch of objects from obj_pool.
440a7344a68SWaiman Long 			 */
441a7344a68SWaiman Long 			for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
442e18328ffSThomas Gleixner 				obj = __alloc_object(&pool_global.objects);
443e18328ffSThomas Gleixner 				hlist_add_head(&obj->node, &pool_to_free.objects);
444e18328ffSThomas Gleixner 				WRITE_ONCE(pool_global.cnt, pool_global.cnt - 1);
445e18328ffSThomas Gleixner 				WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt + 1);
446a7344a68SWaiman Long 			}
447a7344a68SWaiman Long 		}
448636e1970SYang Shi 	} else {
449e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + 1);
450e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_global.objects);
451634d61f4SWaiman Long 		if (lookahead_count) {
452e18328ffSThomas Gleixner 			WRITE_ONCE(pool_global.cnt, pool_global.cnt + lookahead_count);
453634d61f4SWaiman Long 			obj_pool_used -= lookahead_count;
454634d61f4SWaiman Long 			while (lookahead_count) {
455634d61f4SWaiman Long 				hlist_add_head(&objs[--lookahead_count]->node,
456e18328ffSThomas Gleixner 					       &pool_global.objects);
457634d61f4SWaiman Long 			}
458634d61f4SWaiman Long 		}
459636e1970SYang Shi 	}
460d86998b1SWaiman Long 	raw_spin_unlock(&pool_lock);
461636e1970SYang Shi }
462636e1970SYang Shi 
463337fff8bSThomas Gleixner /*
464337fff8bSThomas Gleixner  * Put the object back into the pool and schedule work to free objects
465337fff8bSThomas Gleixner  * if necessary.
4663ac7fe5aSThomas Gleixner  */
4673ac7fe5aSThomas Gleixner static void free_object(struct debug_obj *obj)
4683ac7fe5aSThomas Gleixner {
469a7344a68SWaiman Long 	__free_object(obj);
470e18328ffSThomas Gleixner 	if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
471a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
472a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
473a7344a68SWaiman Long 	}
4743ac7fe5aSThomas Gleixner }
4753ac7fe5aSThomas Gleixner 
476a2a70238SThomas Gleixner static void put_objects(struct hlist_head *list)
47788451f2cSZqiang {
47888451f2cSZqiang 	struct hlist_node *tmp;
47988451f2cSZqiang 	struct debug_obj *obj;
48088451f2cSZqiang 
481a2a70238SThomas Gleixner 	/*
482a2a70238SThomas Gleixner 	 * Using free_object() puts the objects into reuse or schedules
483a2a70238SThomas Gleixner 	 * them for freeing and it get's all the accounting correct.
484a2a70238SThomas Gleixner 	 */
485a2a70238SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, list, node) {
48688451f2cSZqiang 		hlist_del(&obj->node);
487a2a70238SThomas Gleixner 		free_object(obj);
488a2a70238SThomas Gleixner 	}
48988451f2cSZqiang }
490eabb7f1aSwuchi 
49149968cf1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
492a2a70238SThomas Gleixner static int object_cpu_offline(unsigned int cpu)
493a2a70238SThomas Gleixner {
494a2a70238SThomas Gleixner 	/* Remote access is safe as the CPU is dead already */
49518b8afcbSThomas Gleixner 	struct obj_pool *pcp = per_cpu_ptr(&pool_pcpu, cpu);
496eabb7f1aSwuchi 
49718b8afcbSThomas Gleixner 	put_objects(&pcp->objects);
49818b8afcbSThomas Gleixner 	pcp->cnt = 0;
49988451f2cSZqiang 	return 0;
50088451f2cSZqiang }
50188451f2cSZqiang #endif
50288451f2cSZqiang 
50349968cf1SThomas Gleixner /* Out of memory. Free all objects from hash */
5043ac7fe5aSThomas Gleixner static void debug_objects_oom(void)
5053ac7fe5aSThomas Gleixner {
5063ac7fe5aSThomas Gleixner 	struct debug_bucket *db = obj_hash;
507673d62ccSVegard Nossum 	HLIST_HEAD(freelist);
5083ac7fe5aSThomas Gleixner 
509719e4843SFabian Frederick 	pr_warn("Out of memory. ODEBUG disabled\n");
5103ac7fe5aSThomas Gleixner 
51149968cf1SThomas Gleixner 	for (int i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
51249968cf1SThomas Gleixner 		scoped_guard(raw_spinlock_irqsave, &db->lock)
513673d62ccSVegard Nossum 			hlist_move_list(&db->list, &freelist);
514673d62ccSVegard Nossum 
51549968cf1SThomas Gleixner 		put_objects(&freelist);
5163ac7fe5aSThomas Gleixner 	}
5173ac7fe5aSThomas Gleixner }
5183ac7fe5aSThomas Gleixner 
5193ac7fe5aSThomas Gleixner /*
5203ac7fe5aSThomas Gleixner  * We use the pfn of the address for the hash. That way we can check
5213ac7fe5aSThomas Gleixner  * for freed objects simply by checking the affected bucket.
5223ac7fe5aSThomas Gleixner  */
5233ac7fe5aSThomas Gleixner static struct debug_bucket *get_bucket(unsigned long addr)
5243ac7fe5aSThomas Gleixner {
5253ac7fe5aSThomas Gleixner 	unsigned long hash;
5263ac7fe5aSThomas Gleixner 
5273ac7fe5aSThomas Gleixner 	hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
5283ac7fe5aSThomas Gleixner 	return &obj_hash[hash];
5293ac7fe5aSThomas Gleixner }
5303ac7fe5aSThomas Gleixner 
5313ac7fe5aSThomas Gleixner static void debug_print_object(struct debug_obj *obj, char *msg)
5323ac7fe5aSThomas Gleixner {
533aedcade6SStephen Boyd 	const struct debug_obj_descr *descr = obj->descr;
5343ac7fe5aSThomas Gleixner 	static int limit;
5353ac7fe5aSThomas Gleixner 
5368b64d420STetsuo Handa 	/*
5378b64d420STetsuo Handa 	 * Don't report if lookup_object_or_alloc() by the current thread
5388b64d420STetsuo Handa 	 * failed because lookup_object_or_alloc()/debug_objects_oom() by a
5398b64d420STetsuo Handa 	 * concurrent thread turned off debug_objects_enabled and cleared
5408b64d420STetsuo Handa 	 * the hash buckets.
5418b64d420STetsuo Handa 	 */
5428b64d420STetsuo Handa 	if (!debug_objects_enabled)
5438b64d420STetsuo Handa 		return;
5448b64d420STetsuo Handa 
54599777288SStanislaw Gruszka 	if (limit < 5 && descr != descr_test) {
54699777288SStanislaw Gruszka 		void *hint = descr->debug_hint ?
54799777288SStanislaw Gruszka 			descr->debug_hint(obj->object) : NULL;
5483ac7fe5aSThomas Gleixner 		limit++;
549a5d8e467SMathieu Desnoyers 		WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
550c4db2d3bSStephen Boyd 				 "object: %p object type: %s hint: %pS\n",
551a5d8e467SMathieu Desnoyers 			msg, obj_states[obj->state], obj->astate,
552c4db2d3bSStephen Boyd 			obj->object, descr->name, hint);
5533ac7fe5aSThomas Gleixner 	}
5543ac7fe5aSThomas Gleixner 	debug_objects_warnings++;
5553ac7fe5aSThomas Gleixner }
5563ac7fe5aSThomas Gleixner 
5573ac7fe5aSThomas Gleixner /*
5583ac7fe5aSThomas Gleixner  * Try to repair the damage, so we have a better chance to get useful
5593ac7fe5aSThomas Gleixner  * debug output.
5603ac7fe5aSThomas Gleixner  */
561b1e4d9d8SDu, Changbin static bool
562b1e4d9d8SDu, Changbin debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
5633ac7fe5aSThomas Gleixner 		   void * addr, enum debug_obj_state state)
5643ac7fe5aSThomas Gleixner {
565b1e4d9d8SDu, Changbin 	if (fixup && fixup(addr, state)) {
566b1e4d9d8SDu, Changbin 		debug_objects_fixups++;
567b1e4d9d8SDu, Changbin 		return true;
568b1e4d9d8SDu, Changbin 	}
569b1e4d9d8SDu, Changbin 	return false;
5703ac7fe5aSThomas Gleixner }
5713ac7fe5aSThomas Gleixner 
5723ac7fe5aSThomas Gleixner static void debug_object_is_on_stack(void *addr, int onstack)
5733ac7fe5aSThomas Gleixner {
5743ac7fe5aSThomas Gleixner 	int is_on_stack;
5753ac7fe5aSThomas Gleixner 	static int limit;
5763ac7fe5aSThomas Gleixner 
5773ac7fe5aSThomas Gleixner 	if (limit > 4)
5783ac7fe5aSThomas Gleixner 		return;
5793ac7fe5aSThomas Gleixner 
5808b05c7e6SFUJITA Tomonori 	is_on_stack = object_is_on_stack(addr);
5813ac7fe5aSThomas Gleixner 	if (is_on_stack == onstack)
5823ac7fe5aSThomas Gleixner 		return;
5833ac7fe5aSThomas Gleixner 
5843ac7fe5aSThomas Gleixner 	limit++;
5853ac7fe5aSThomas Gleixner 	if (is_on_stack)
586fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
587fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
5883ac7fe5aSThomas Gleixner 	else
589fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
590fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
591fc91a3c4SJoel Fernandes (Google) 
5923ac7fe5aSThomas Gleixner 	WARN_ON(1);
5933ac7fe5aSThomas Gleixner }
5943ac7fe5aSThomas Gleixner 
59563a75969SThomas Gleixner static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
59663a75969SThomas Gleixner 						const struct debug_obj_descr *descr,
59763a75969SThomas Gleixner 						bool onstack, bool alloc_ifstatic)
59863a75969SThomas Gleixner {
59963a75969SThomas Gleixner 	struct debug_obj *obj = lookup_object(addr, b);
60063a75969SThomas Gleixner 	enum debug_obj_state state = ODEBUG_STATE_NONE;
60163a75969SThomas Gleixner 
60263a75969SThomas Gleixner 	if (likely(obj))
60363a75969SThomas Gleixner 		return obj;
60463a75969SThomas Gleixner 
60563a75969SThomas Gleixner 	/*
60663a75969SThomas Gleixner 	 * debug_object_init() unconditionally allocates untracked
60763a75969SThomas Gleixner 	 * objects. It does not matter whether it is a static object or
60863a75969SThomas Gleixner 	 * not.
60963a75969SThomas Gleixner 	 *
61063a75969SThomas Gleixner 	 * debug_object_assert_init() and debug_object_activate() allow
61163a75969SThomas Gleixner 	 * allocation only if the descriptor callback confirms that the
61263a75969SThomas Gleixner 	 * object is static and considered initialized. For non-static
61363a75969SThomas Gleixner 	 * objects the allocation needs to be done from the fixup callback.
61463a75969SThomas Gleixner 	 */
61563a75969SThomas Gleixner 	if (unlikely(alloc_ifstatic)) {
61663a75969SThomas Gleixner 		if (!descr->is_static_object || !descr->is_static_object(addr))
61763a75969SThomas Gleixner 			return ERR_PTR(-ENOENT);
61863a75969SThomas Gleixner 		/* Statically allocated objects are considered initialized */
61963a75969SThomas Gleixner 		state = ODEBUG_STATE_INIT;
62063a75969SThomas Gleixner 	}
62163a75969SThomas Gleixner 
62263a75969SThomas Gleixner 	obj = alloc_object(addr, b, descr);
62363a75969SThomas Gleixner 	if (likely(obj)) {
62463a75969SThomas Gleixner 		obj->state = state;
62563a75969SThomas Gleixner 		debug_object_is_on_stack(addr, onstack);
62663a75969SThomas Gleixner 		return obj;
62763a75969SThomas Gleixner 	}
62863a75969SThomas Gleixner 
62963a75969SThomas Gleixner 	/* Out of memory. Do the cleanup outside of the locked region */
630661cc28bSThomas Gleixner 	debug_objects_enabled = false;
63163a75969SThomas Gleixner 	return NULL;
63263a75969SThomas Gleixner }
63363a75969SThomas Gleixner 
6340af462f1SThomas Gleixner static void debug_objects_fill_pool(void)
6350af462f1SThomas Gleixner {
636d8c6cd3aSZhen Lei 	if (unlikely(!obj_cache))
637d8c6cd3aSZhen Lei 		return;
638d8c6cd3aSZhen Lei 
639*96a9a042SThomas Gleixner 	if (likely(!pool_should_refill(&pool_global)))
640d8c6cd3aSZhen Lei 		return;
641d8c6cd3aSZhen Lei 
642d8c6cd3aSZhen Lei 	/* Try reusing objects from obj_to_free_list */
643d8c6cd3aSZhen Lei 	fill_pool_from_freelist();
644d8c6cd3aSZhen Lei 
645*96a9a042SThomas Gleixner 	if (likely(!pool_should_refill(&pool_global)))
646d8c6cd3aSZhen Lei 		return;
647d8c6cd3aSZhen Lei 
6480af462f1SThomas Gleixner 	/*
6490af462f1SThomas Gleixner 	 * On RT enabled kernels the pool refill must happen in preemptible
6500cce06baSPeter Zijlstra 	 * context -- for !RT kernels we rely on the fact that spinlock_t and
6510cce06baSPeter Zijlstra 	 * raw_spinlock_t are basically the same type and this lock-type
6520cce06baSPeter Zijlstra 	 * inversion works just fine.
6530af462f1SThomas Gleixner 	 */
6540cce06baSPeter Zijlstra 	if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible()) {
6550cce06baSPeter Zijlstra 		/*
6560cce06baSPeter Zijlstra 		 * Annotate away the spinlock_t inside raw_spinlock_t warning
6570cce06baSPeter Zijlstra 		 * by temporarily raising the wait-type to WAIT_SLEEP, matching
6580cce06baSPeter Zijlstra 		 * the preemptible() condition above.
6590cce06baSPeter Zijlstra 		 */
6600cce06baSPeter Zijlstra 		static DEFINE_WAIT_OVERRIDE_MAP(fill_pool_map, LD_WAIT_SLEEP);
6610cce06baSPeter Zijlstra 		lock_map_acquire_try(&fill_pool_map);
6620af462f1SThomas Gleixner 		fill_pool();
6630cce06baSPeter Zijlstra 		lock_map_release(&fill_pool_map);
6640cce06baSPeter Zijlstra 	}
6650af462f1SThomas Gleixner }
6660af462f1SThomas Gleixner 
6673ac7fe5aSThomas Gleixner static void
668aedcade6SStephen Boyd __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
6693ac7fe5aSThomas Gleixner {
6709bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
6713ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
6723ac7fe5aSThomas Gleixner 	unsigned long flags;
6733ac7fe5aSThomas Gleixner 
6740af462f1SThomas Gleixner 	debug_objects_fill_pool();
67550db04ddSVegard Nossum 
6763ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
6773ac7fe5aSThomas Gleixner 
678aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
6793ac7fe5aSThomas Gleixner 
68063a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
68163a75969SThomas Gleixner 	if (unlikely(!obj)) {
682aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
6833ac7fe5aSThomas Gleixner 		debug_objects_oom();
6843ac7fe5aSThomas Gleixner 		return;
6853ac7fe5aSThomas Gleixner 	}
6863ac7fe5aSThomas Gleixner 
6873ac7fe5aSThomas Gleixner 	switch (obj->state) {
6883ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
6893ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
6903ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
6913ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_INIT;
692aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
693d5f34153SWaiman Long 		return;
6943ac7fe5aSThomas Gleixner 	default:
6953ac7fe5aSThomas Gleixner 		break;
6963ac7fe5aSThomas Gleixner 	}
6973ac7fe5aSThomas Gleixner 
6989bb63626SAndrzej Hajda 	o = *obj;
699aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
7009bb63626SAndrzej Hajda 	debug_print_object(&o, "init");
7019bb63626SAndrzej Hajda 
7029bb63626SAndrzej Hajda 	if (o.state == ODEBUG_STATE_ACTIVE)
7039bb63626SAndrzej Hajda 		debug_object_fixup(descr->fixup_init, addr, o.state);
7043ac7fe5aSThomas Gleixner }
7053ac7fe5aSThomas Gleixner 
7063ac7fe5aSThomas Gleixner /**
7073ac7fe5aSThomas Gleixner  * debug_object_init - debug checks when an object is initialized
7083ac7fe5aSThomas Gleixner  * @addr:	address of the object
7093ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7103ac7fe5aSThomas Gleixner  */
711aedcade6SStephen Boyd void debug_object_init(void *addr, const struct debug_obj_descr *descr)
7123ac7fe5aSThomas Gleixner {
7133ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7143ac7fe5aSThomas Gleixner 		return;
7153ac7fe5aSThomas Gleixner 
7163ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 0);
7173ac7fe5aSThomas Gleixner }
718f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init);
7193ac7fe5aSThomas Gleixner 
7203ac7fe5aSThomas Gleixner /**
7213ac7fe5aSThomas Gleixner  * debug_object_init_on_stack - debug checks when an object on stack is
7223ac7fe5aSThomas Gleixner  *				initialized
7233ac7fe5aSThomas Gleixner  * @addr:	address of the object
7243ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7253ac7fe5aSThomas Gleixner  */
726aedcade6SStephen Boyd void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr)
7273ac7fe5aSThomas Gleixner {
7283ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7293ac7fe5aSThomas Gleixner 		return;
7303ac7fe5aSThomas Gleixner 
7313ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 1);
7323ac7fe5aSThomas Gleixner }
733f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
7343ac7fe5aSThomas Gleixner 
7353ac7fe5aSThomas Gleixner /**
7363ac7fe5aSThomas Gleixner  * debug_object_activate - debug checks when an object is activated
7373ac7fe5aSThomas Gleixner  * @addr:	address of the object
7383ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
739b778ae25SPaul E. McKenney  * Returns 0 for success, -EINVAL for check failed.
7403ac7fe5aSThomas Gleixner  */
741aedcade6SStephen Boyd int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
7423ac7fe5aSThomas Gleixner {
74363a75969SThomas Gleixner 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
7443ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
7453ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
7463ac7fe5aSThomas Gleixner 	unsigned long flags;
7473ac7fe5aSThomas Gleixner 
7483ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
749b778ae25SPaul E. McKenney 		return 0;
7503ac7fe5aSThomas Gleixner 
7510af462f1SThomas Gleixner 	debug_objects_fill_pool();
7520af462f1SThomas Gleixner 
7533ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
7543ac7fe5aSThomas Gleixner 
755aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
7563ac7fe5aSThomas Gleixner 
75763a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, false, true);
7589bb63626SAndrzej Hajda 	if (unlikely(!obj)) {
7599bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
7609bb63626SAndrzej Hajda 		debug_objects_oom();
7619bb63626SAndrzej Hajda 		return 0;
7629bb63626SAndrzej Hajda 	} else if (likely(!IS_ERR(obj))) {
7633ac7fe5aSThomas Gleixner 		switch (obj->state) {
7649bb63626SAndrzej Hajda 		case ODEBUG_STATE_ACTIVE:
7659bb63626SAndrzej Hajda 		case ODEBUG_STATE_DESTROYED:
7669bb63626SAndrzej Hajda 			o = *obj;
7679bb63626SAndrzej Hajda 			break;
7683ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
7693ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
7703ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_ACTIVE;
7719bb63626SAndrzej Hajda 			fallthrough;
7723ac7fe5aSThomas Gleixner 		default:
773aef9cb05SThomas Gleixner 			raw_spin_unlock_irqrestore(&db->lock, flags);
774b778ae25SPaul E. McKenney 			return 0;
7753ac7fe5aSThomas Gleixner 		}
7769bb63626SAndrzej Hajda 	}
77763a75969SThomas Gleixner 
7789bb63626SAndrzej Hajda 	raw_spin_unlock_irqrestore(&db->lock, flags);
77963a75969SThomas Gleixner 	debug_print_object(&o, "activate");
7809bb63626SAndrzej Hajda 
7819bb63626SAndrzej Hajda 	switch (o.state) {
7829bb63626SAndrzej Hajda 	case ODEBUG_STATE_ACTIVE:
7839bb63626SAndrzej Hajda 	case ODEBUG_STATE_NOTAVAILABLE:
7849bb63626SAndrzej Hajda 		if (debug_object_fixup(descr->fixup_activate, addr, o.state))
7859bb63626SAndrzej Hajda 			return 0;
7869bb63626SAndrzej Hajda 		fallthrough;
7879bb63626SAndrzej Hajda 	default:
7889bb63626SAndrzej Hajda 		return -EINVAL;
7899bb63626SAndrzej Hajda 	}
79063a75969SThomas Gleixner }
791f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_activate);
7923ac7fe5aSThomas Gleixner 
7933ac7fe5aSThomas Gleixner /**
7943ac7fe5aSThomas Gleixner  * debug_object_deactivate - debug checks when an object is deactivated
7953ac7fe5aSThomas Gleixner  * @addr:	address of the object
7963ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7973ac7fe5aSThomas Gleixner  */
798aedcade6SStephen Boyd void debug_object_deactivate(void *addr, const struct debug_obj_descr *descr)
7993ac7fe5aSThomas Gleixner {
8009bb63626SAndrzej Hajda 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
8013ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8023ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
8033ac7fe5aSThomas Gleixner 	unsigned long flags;
8043ac7fe5aSThomas Gleixner 
8053ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8063ac7fe5aSThomas Gleixner 		return;
8073ac7fe5aSThomas Gleixner 
8083ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8093ac7fe5aSThomas Gleixner 
810aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8113ac7fe5aSThomas Gleixner 
8123ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
8133ac7fe5aSThomas Gleixner 	if (obj) {
8143ac7fe5aSThomas Gleixner 		switch (obj->state) {
8159bb63626SAndrzej Hajda 		case ODEBUG_STATE_DESTROYED:
8169bb63626SAndrzej Hajda 			break;
8173ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
8183ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
8193ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_ACTIVE:
8209bb63626SAndrzej Hajda 			if (obj->astate)
8219bb63626SAndrzej Hajda 				break;
8223ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_INACTIVE;
8239bb63626SAndrzej Hajda 			fallthrough;
8243ac7fe5aSThomas Gleixner 		default:
8259bb63626SAndrzej Hajda 			raw_spin_unlock_irqrestore(&db->lock, flags);
8269bb63626SAndrzej Hajda 			return;
8273ac7fe5aSThomas Gleixner 		}
8289bb63626SAndrzej Hajda 		o = *obj;
829d5f34153SWaiman Long 	}
830d5f34153SWaiman Long 
831d5f34153SWaiman Long 	raw_spin_unlock_irqrestore(&db->lock, flags);
8323ac7fe5aSThomas Gleixner 	debug_print_object(&o, "deactivate");
8333ac7fe5aSThomas Gleixner }
834f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_deactivate);
8353ac7fe5aSThomas Gleixner 
8363ac7fe5aSThomas Gleixner /**
8373ac7fe5aSThomas Gleixner  * debug_object_destroy - debug checks when an object is destroyed
8383ac7fe5aSThomas Gleixner  * @addr:	address of the object
8393ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
8403ac7fe5aSThomas Gleixner  */
841aedcade6SStephen Boyd void debug_object_destroy(void *addr, const struct debug_obj_descr *descr)
8423ac7fe5aSThomas Gleixner {
8439bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
8443ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8453ac7fe5aSThomas Gleixner 	unsigned long flags;
8463ac7fe5aSThomas Gleixner 
8473ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8483ac7fe5aSThomas Gleixner 		return;
8493ac7fe5aSThomas Gleixner 
8503ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8513ac7fe5aSThomas Gleixner 
852aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8533ac7fe5aSThomas Gleixner 
8543ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
8559bb63626SAndrzej Hajda 	if (!obj) {
8569bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
8579bb63626SAndrzej Hajda 		return;
8589bb63626SAndrzej Hajda 	}
8593ac7fe5aSThomas Gleixner 
8603ac7fe5aSThomas Gleixner 	switch (obj->state) {
8619bb63626SAndrzej Hajda 	case ODEBUG_STATE_ACTIVE:
8629bb63626SAndrzej Hajda 	case ODEBUG_STATE_DESTROYED:
8639bb63626SAndrzej Hajda 		break;
8643ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
8653ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
8663ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
8673ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_DESTROYED;
8689bb63626SAndrzej Hajda 		fallthrough;
8693ac7fe5aSThomas Gleixner 	default:
870aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
8719bb63626SAndrzej Hajda 		return;
8729bb63626SAndrzej Hajda 	}
8739bb63626SAndrzej Hajda 
8749bb63626SAndrzej Hajda 	o = *obj;
8759bb63626SAndrzej Hajda 	raw_spin_unlock_irqrestore(&db->lock, flags);
8769bb63626SAndrzej Hajda 	debug_print_object(&o, "destroy");
8779bb63626SAndrzej Hajda 
8789bb63626SAndrzej Hajda 	if (o.state == ODEBUG_STATE_ACTIVE)
8799bb63626SAndrzej Hajda 		debug_object_fixup(descr->fixup_destroy, addr, o.state);
8803ac7fe5aSThomas Gleixner }
881f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_destroy);
8823ac7fe5aSThomas Gleixner 
8833ac7fe5aSThomas Gleixner /**
8843ac7fe5aSThomas Gleixner  * debug_object_free - debug checks when an object is freed
8853ac7fe5aSThomas Gleixner  * @addr:	address of the object
8863ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
8873ac7fe5aSThomas Gleixner  */
888aedcade6SStephen Boyd void debug_object_free(void *addr, const struct debug_obj_descr *descr)
8893ac7fe5aSThomas Gleixner {
8909bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
8913ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8923ac7fe5aSThomas Gleixner 	unsigned long flags;
8933ac7fe5aSThomas Gleixner 
8943ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8953ac7fe5aSThomas Gleixner 		return;
8963ac7fe5aSThomas Gleixner 
8973ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8983ac7fe5aSThomas Gleixner 
899aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
9003ac7fe5aSThomas Gleixner 
9013ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
9029bb63626SAndrzej Hajda 	if (!obj) {
9039bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
9049bb63626SAndrzej Hajda 		return;
9059bb63626SAndrzej Hajda 	}
9063ac7fe5aSThomas Gleixner 
9073ac7fe5aSThomas Gleixner 	switch (obj->state) {
9083ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
9099bb63626SAndrzej Hajda 		break;
9103ac7fe5aSThomas Gleixner 	default:
9113ac7fe5aSThomas Gleixner 		hlist_del(&obj->node);
912aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
9133ac7fe5aSThomas Gleixner 		free_object(obj);
914673d62ccSVegard Nossum 		return;
9153ac7fe5aSThomas Gleixner 	}
9169bb63626SAndrzej Hajda 
9179bb63626SAndrzej Hajda 	o = *obj;
918aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
9199bb63626SAndrzej Hajda 	debug_print_object(&o, "free");
9209bb63626SAndrzej Hajda 
9219bb63626SAndrzej Hajda 	debug_object_fixup(descr->fixup_free, addr, o.state);
9223ac7fe5aSThomas Gleixner }
923f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_free);
9243ac7fe5aSThomas Gleixner 
925a5d8e467SMathieu Desnoyers /**
926b84d435cSChristine Chan  * debug_object_assert_init - debug checks when object should be init-ed
927b84d435cSChristine Chan  * @addr:	address of the object
928b84d435cSChristine Chan  * @descr:	pointer to an object specific debug description structure
929b84d435cSChristine Chan  */
930aedcade6SStephen Boyd void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
931b84d435cSChristine Chan {
93263a75969SThomas Gleixner 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
933b84d435cSChristine Chan 	struct debug_bucket *db;
934b84d435cSChristine Chan 	struct debug_obj *obj;
935b84d435cSChristine Chan 	unsigned long flags;
936b84d435cSChristine Chan 
937b84d435cSChristine Chan 	if (!debug_objects_enabled)
938b84d435cSChristine Chan 		return;
939b84d435cSChristine Chan 
9400af462f1SThomas Gleixner 	debug_objects_fill_pool();
9410af462f1SThomas Gleixner 
942b84d435cSChristine Chan 	db = get_bucket((unsigned long) addr);
943b84d435cSChristine Chan 
944b84d435cSChristine Chan 	raw_spin_lock_irqsave(&db->lock, flags);
94563a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, false, true);
946b84d435cSChristine Chan 	raw_spin_unlock_irqrestore(&db->lock, flags);
94763a75969SThomas Gleixner 	if (likely(!IS_ERR_OR_NULL(obj)))
94863a75969SThomas Gleixner 		return;
94963a75969SThomas Gleixner 
95063a75969SThomas Gleixner 	/* If NULL the allocation has hit OOM */
95163a75969SThomas Gleixner 	if (!obj) {
95263a75969SThomas Gleixner 		debug_objects_oom();
953b84d435cSChristine Chan 		return;
954b84d435cSChristine Chan 	}
955b84d435cSChristine Chan 
95663a75969SThomas Gleixner 	/* Object is neither tracked nor static. It's not initialized. */
95763a75969SThomas Gleixner 	debug_print_object(&o, "assert_init");
95863a75969SThomas Gleixner 	debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
959b84d435cSChristine Chan }
960f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_assert_init);
961b84d435cSChristine Chan 
962b84d435cSChristine Chan /**
963a5d8e467SMathieu Desnoyers  * debug_object_active_state - debug checks object usage state machine
964a5d8e467SMathieu Desnoyers  * @addr:	address of the object
965a5d8e467SMathieu Desnoyers  * @descr:	pointer to an object specific debug description structure
966a5d8e467SMathieu Desnoyers  * @expect:	expected state
967a5d8e467SMathieu Desnoyers  * @next:	state to move to if expected state is found
968a5d8e467SMathieu Desnoyers  */
969a5d8e467SMathieu Desnoyers void
970aedcade6SStephen Boyd debug_object_active_state(void *addr, const struct debug_obj_descr *descr,
971a5d8e467SMathieu Desnoyers 			  unsigned int expect, unsigned int next)
972a5d8e467SMathieu Desnoyers {
9739bb63626SAndrzej Hajda 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
974a5d8e467SMathieu Desnoyers 	struct debug_bucket *db;
975a5d8e467SMathieu Desnoyers 	struct debug_obj *obj;
976a5d8e467SMathieu Desnoyers 	unsigned long flags;
977a5d8e467SMathieu Desnoyers 
978a5d8e467SMathieu Desnoyers 	if (!debug_objects_enabled)
979a5d8e467SMathieu Desnoyers 		return;
980a5d8e467SMathieu Desnoyers 
981a5d8e467SMathieu Desnoyers 	db = get_bucket((unsigned long) addr);
982a5d8e467SMathieu Desnoyers 
983a5d8e467SMathieu Desnoyers 	raw_spin_lock_irqsave(&db->lock, flags);
984a5d8e467SMathieu Desnoyers 
985a5d8e467SMathieu Desnoyers 	obj = lookup_object(addr, db);
986a5d8e467SMathieu Desnoyers 	if (obj) {
987a5d8e467SMathieu Desnoyers 		switch (obj->state) {
988a5d8e467SMathieu Desnoyers 		case ODEBUG_STATE_ACTIVE:
9899bb63626SAndrzej Hajda 			if (obj->astate != expect)
990a5d8e467SMathieu Desnoyers 				break;
9919bb63626SAndrzej Hajda 			obj->astate = next;
9929bb63626SAndrzej Hajda 			raw_spin_unlock_irqrestore(&db->lock, flags);
9939bb63626SAndrzej Hajda 			return;
994a5d8e467SMathieu Desnoyers 		default:
995a5d8e467SMathieu Desnoyers 			break;
996a5d8e467SMathieu Desnoyers 		}
9979bb63626SAndrzej Hajda 		o = *obj;
998d5f34153SWaiman Long 	}
999d5f34153SWaiman Long 
1000d5f34153SWaiman Long 	raw_spin_unlock_irqrestore(&db->lock, flags);
1001a5d8e467SMathieu Desnoyers 	debug_print_object(&o, "active_state");
1002a5d8e467SMathieu Desnoyers }
1003f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_active_state);
1004a5d8e467SMathieu Desnoyers 
10053ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
10063ac7fe5aSThomas Gleixner static void __debug_check_no_obj_freed(const void *address, unsigned long size)
10073ac7fe5aSThomas Gleixner {
10083ac7fe5aSThomas Gleixner 	unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
10099bb63626SAndrzej Hajda 	int cnt, objs_checked = 0;
10109bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
10113ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
10121ea9b98bSYang Shi 	struct hlist_node *tmp;
10133ac7fe5aSThomas Gleixner 
10143ac7fe5aSThomas Gleixner 	saddr = (unsigned long) address;
10153ac7fe5aSThomas Gleixner 	eaddr = saddr + size;
10163ac7fe5aSThomas Gleixner 	paddr = saddr & ODEBUG_CHUNK_MASK;
10173ac7fe5aSThomas Gleixner 	chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
10183ac7fe5aSThomas Gleixner 	chunks >>= ODEBUG_CHUNK_SHIFT;
10193ac7fe5aSThomas Gleixner 
10203ac7fe5aSThomas Gleixner 	for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
10213ac7fe5aSThomas Gleixner 		db = get_bucket(paddr);
10223ac7fe5aSThomas Gleixner 
10233ac7fe5aSThomas Gleixner repeat:
10243ac7fe5aSThomas Gleixner 		cnt = 0;
1025aef9cb05SThomas Gleixner 		raw_spin_lock_irqsave(&db->lock, flags);
1026b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
10273ac7fe5aSThomas Gleixner 			cnt++;
10283ac7fe5aSThomas Gleixner 			oaddr = (unsigned long) obj->object;
10293ac7fe5aSThomas Gleixner 			if (oaddr < saddr || oaddr >= eaddr)
10303ac7fe5aSThomas Gleixner 				continue;
10313ac7fe5aSThomas Gleixner 
10323ac7fe5aSThomas Gleixner 			switch (obj->state) {
10333ac7fe5aSThomas Gleixner 			case ODEBUG_STATE_ACTIVE:
10349bb63626SAndrzej Hajda 				o = *obj;
1035aef9cb05SThomas Gleixner 				raw_spin_unlock_irqrestore(&db->lock, flags);
10369bb63626SAndrzej Hajda 				debug_print_object(&o, "free");
10379bb63626SAndrzej Hajda 				debug_object_fixup(o.descr->fixup_free, (void *)oaddr, o.state);
10383ac7fe5aSThomas Gleixner 				goto repeat;
10393ac7fe5aSThomas Gleixner 			default:
10403ac7fe5aSThomas Gleixner 				hlist_del(&obj->node);
1041a7344a68SWaiman Long 				__free_object(obj);
10423ac7fe5aSThomas Gleixner 				break;
10433ac7fe5aSThomas Gleixner 			}
10443ac7fe5aSThomas Gleixner 		}
1045aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
1046673d62ccSVegard Nossum 
10473ac7fe5aSThomas Gleixner 		if (cnt > debug_objects_maxchain)
10483ac7fe5aSThomas Gleixner 			debug_objects_maxchain = cnt;
1049bd9dcd04SYang Shi 
1050bd9dcd04SYang Shi 		objs_checked += cnt;
10513ac7fe5aSThomas Gleixner 	}
1052bd9dcd04SYang Shi 
1053bd9dcd04SYang Shi 	if (objs_checked > debug_objects_maxchecked)
1054bd9dcd04SYang Shi 		debug_objects_maxchecked = objs_checked;
10551ea9b98bSYang Shi 
10561ea9b98bSYang Shi 	/* Schedule work to actually kmem_cache_free() objects */
1057e18328ffSThomas Gleixner 	if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
1058a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
1059a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
1060a7344a68SWaiman Long 	}
10613ac7fe5aSThomas Gleixner }
10623ac7fe5aSThomas Gleixner 
10633ac7fe5aSThomas Gleixner void debug_check_no_obj_freed(const void *address, unsigned long size)
10643ac7fe5aSThomas Gleixner {
10653ac7fe5aSThomas Gleixner 	if (debug_objects_enabled)
10663ac7fe5aSThomas Gleixner 		__debug_check_no_obj_freed(address, size);
10673ac7fe5aSThomas Gleixner }
10683ac7fe5aSThomas Gleixner #endif
10693ac7fe5aSThomas Gleixner 
10703ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_FS
10713ac7fe5aSThomas Gleixner 
10723ac7fe5aSThomas Gleixner static int debug_stats_show(struct seq_file *m, void *v)
10733ac7fe5aSThomas Gleixner {
1074d86998b1SWaiman Long 	int cpu, obj_percpu_free = 0;
1075d86998b1SWaiman Long 
1076d86998b1SWaiman Long 	for_each_possible_cpu(cpu)
107718b8afcbSThomas Gleixner 		obj_percpu_free += per_cpu(pool_pcpu.cnt, cpu);
1078d86998b1SWaiman Long 
10793ac7fe5aSThomas Gleixner 	seq_printf(m, "max_chain     :%d\n", debug_objects_maxchain);
1080bd9dcd04SYang Shi 	seq_printf(m, "max_checked   :%d\n", debug_objects_maxchecked);
10813ac7fe5aSThomas Gleixner 	seq_printf(m, "warnings      :%d\n", debug_objects_warnings);
10823ac7fe5aSThomas Gleixner 	seq_printf(m, "fixups        :%d\n", debug_objects_fixups);
1083e18328ffSThomas Gleixner 	seq_printf(m, "pool_free     :%d\n", pool_count(&pool_global) + obj_percpu_free);
1084d86998b1SWaiman Long 	seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
10853ac7fe5aSThomas Gleixner 	seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
1086d86998b1SWaiman Long 	seq_printf(m, "pool_used     :%d\n", obj_pool_used - obj_percpu_free);
10873ac7fe5aSThomas Gleixner 	seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
1088e18328ffSThomas Gleixner 	seq_printf(m, "on_free_list  :%d\n", pool_count(&pool_to_free));
10890cad93c3SWaiman Long 	seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
10900cad93c3SWaiman Long 	seq_printf(m, "objs_freed    :%d\n", debug_objects_freed);
10913ac7fe5aSThomas Gleixner 	return 0;
10923ac7fe5aSThomas Gleixner }
10930f85c480SQinglang Miao DEFINE_SHOW_ATTRIBUTE(debug_stats);
10943ac7fe5aSThomas Gleixner 
10953ac7fe5aSThomas Gleixner static int __init debug_objects_init_debugfs(void)
10963ac7fe5aSThomas Gleixner {
1097fecb0d95SGreg Kroah-Hartman 	struct dentry *dbgdir;
10983ac7fe5aSThomas Gleixner 
10993ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
11003ac7fe5aSThomas Gleixner 		return 0;
11013ac7fe5aSThomas Gleixner 
11023ac7fe5aSThomas Gleixner 	dbgdir = debugfs_create_dir("debug_objects", NULL);
11033ac7fe5aSThomas Gleixner 
1104fecb0d95SGreg Kroah-Hartman 	debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
11053ac7fe5aSThomas Gleixner 
11063ac7fe5aSThomas Gleixner 	return 0;
11073ac7fe5aSThomas Gleixner }
11083ac7fe5aSThomas Gleixner __initcall(debug_objects_init_debugfs);
11093ac7fe5aSThomas Gleixner 
11103ac7fe5aSThomas Gleixner #else
11113ac7fe5aSThomas Gleixner static inline void debug_objects_init_debugfs(void) { }
11123ac7fe5aSThomas Gleixner #endif
11133ac7fe5aSThomas Gleixner 
11143ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
11153ac7fe5aSThomas Gleixner 
11163ac7fe5aSThomas Gleixner /* Random data structure for the self test */
11173ac7fe5aSThomas Gleixner struct self_test {
11183ac7fe5aSThomas Gleixner 	unsigned long	dummy1[6];
11193ac7fe5aSThomas Gleixner 	int		static_init;
11203ac7fe5aSThomas Gleixner 	unsigned long	dummy2[3];
11213ac7fe5aSThomas Gleixner };
11223ac7fe5aSThomas Gleixner 
1123aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test;
11243ac7fe5aSThomas Gleixner 
1125b9fdac7fSDu, Changbin static bool __init is_static_object(void *addr)
1126b9fdac7fSDu, Changbin {
1127b9fdac7fSDu, Changbin 	struct self_test *obj = addr;
1128b9fdac7fSDu, Changbin 
1129b9fdac7fSDu, Changbin 	return obj->static_init;
1130b9fdac7fSDu, Changbin }
1131b9fdac7fSDu, Changbin 
11323ac7fe5aSThomas Gleixner /*
11333ac7fe5aSThomas Gleixner  * fixup_init is called when:
11343ac7fe5aSThomas Gleixner  * - an active object is initialized
11353ac7fe5aSThomas Gleixner  */
1136b1e4d9d8SDu, Changbin static bool __init fixup_init(void *addr, enum debug_obj_state state)
11373ac7fe5aSThomas Gleixner {
11383ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11393ac7fe5aSThomas Gleixner 
11403ac7fe5aSThomas Gleixner 	switch (state) {
11413ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11423ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11433ac7fe5aSThomas Gleixner 		debug_object_init(obj, &descr_type_test);
1144b1e4d9d8SDu, Changbin 		return true;
11453ac7fe5aSThomas Gleixner 	default:
1146b1e4d9d8SDu, Changbin 		return false;
11473ac7fe5aSThomas Gleixner 	}
11483ac7fe5aSThomas Gleixner }
11493ac7fe5aSThomas Gleixner 
11503ac7fe5aSThomas Gleixner /*
11513ac7fe5aSThomas Gleixner  * fixup_activate is called when:
11523ac7fe5aSThomas Gleixner  * - an active object is activated
1153b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
11543ac7fe5aSThomas Gleixner  */
1155b1e4d9d8SDu, Changbin static bool __init fixup_activate(void *addr, enum debug_obj_state state)
11563ac7fe5aSThomas Gleixner {
11573ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11583ac7fe5aSThomas Gleixner 
11593ac7fe5aSThomas Gleixner 	switch (state) {
11603ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
1161b1e4d9d8SDu, Changbin 		return true;
11623ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11633ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11643ac7fe5aSThomas Gleixner 		debug_object_activate(obj, &descr_type_test);
1165b1e4d9d8SDu, Changbin 		return true;
11663ac7fe5aSThomas Gleixner 
11673ac7fe5aSThomas Gleixner 	default:
1168b1e4d9d8SDu, Changbin 		return false;
11693ac7fe5aSThomas Gleixner 	}
11703ac7fe5aSThomas Gleixner }
11713ac7fe5aSThomas Gleixner 
11723ac7fe5aSThomas Gleixner /*
11733ac7fe5aSThomas Gleixner  * fixup_destroy is called when:
11743ac7fe5aSThomas Gleixner  * - an active object is destroyed
11753ac7fe5aSThomas Gleixner  */
1176b1e4d9d8SDu, Changbin static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
11773ac7fe5aSThomas Gleixner {
11783ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11793ac7fe5aSThomas Gleixner 
11803ac7fe5aSThomas Gleixner 	switch (state) {
11813ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11823ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11833ac7fe5aSThomas Gleixner 		debug_object_destroy(obj, &descr_type_test);
1184b1e4d9d8SDu, Changbin 		return true;
11853ac7fe5aSThomas Gleixner 	default:
1186b1e4d9d8SDu, Changbin 		return false;
11873ac7fe5aSThomas Gleixner 	}
11883ac7fe5aSThomas Gleixner }
11893ac7fe5aSThomas Gleixner 
11903ac7fe5aSThomas Gleixner /*
11913ac7fe5aSThomas Gleixner  * fixup_free is called when:
11923ac7fe5aSThomas Gleixner  * - an active object is freed
11933ac7fe5aSThomas Gleixner  */
1194b1e4d9d8SDu, Changbin static bool __init fixup_free(void *addr, enum debug_obj_state state)
11953ac7fe5aSThomas Gleixner {
11963ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11973ac7fe5aSThomas Gleixner 
11983ac7fe5aSThomas Gleixner 	switch (state) {
11993ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
12003ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
12013ac7fe5aSThomas Gleixner 		debug_object_free(obj, &descr_type_test);
1202b1e4d9d8SDu, Changbin 		return true;
12033ac7fe5aSThomas Gleixner 	default:
1204b1e4d9d8SDu, Changbin 		return false;
12053ac7fe5aSThomas Gleixner 	}
12063ac7fe5aSThomas Gleixner }
12073ac7fe5aSThomas Gleixner 
12081fb2f77cSHenrik Kretzschmar static int __init
12093ac7fe5aSThomas Gleixner check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
12103ac7fe5aSThomas Gleixner {
12113ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
12123ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
12133ac7fe5aSThomas Gleixner 	unsigned long flags;
12143ac7fe5aSThomas Gleixner 	int res = -EINVAL;
12153ac7fe5aSThomas Gleixner 
12163ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
12173ac7fe5aSThomas Gleixner 
1218aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
12193ac7fe5aSThomas Gleixner 
12203ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
12213ac7fe5aSThomas Gleixner 	if (!obj && state != ODEBUG_STATE_NONE) {
12225cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
12233ac7fe5aSThomas Gleixner 		goto out;
12243ac7fe5aSThomas Gleixner 	}
12253ac7fe5aSThomas Gleixner 	if (obj && obj->state != state) {
12265cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
12273ac7fe5aSThomas Gleixner 		       obj->state, state);
12283ac7fe5aSThomas Gleixner 		goto out;
12293ac7fe5aSThomas Gleixner 	}
12303ac7fe5aSThomas Gleixner 	if (fixups != debug_objects_fixups) {
12315cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
12323ac7fe5aSThomas Gleixner 		       fixups, debug_objects_fixups);
12333ac7fe5aSThomas Gleixner 		goto out;
12343ac7fe5aSThomas Gleixner 	}
12353ac7fe5aSThomas Gleixner 	if (warnings != debug_objects_warnings) {
12365cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
12373ac7fe5aSThomas Gleixner 		       warnings, debug_objects_warnings);
12383ac7fe5aSThomas Gleixner 		goto out;
12393ac7fe5aSThomas Gleixner 	}
12403ac7fe5aSThomas Gleixner 	res = 0;
12413ac7fe5aSThomas Gleixner out:
1242aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
12433ac7fe5aSThomas Gleixner 	if (res)
1244661cc28bSThomas Gleixner 		debug_objects_enabled = false;
12453ac7fe5aSThomas Gleixner 	return res;
12463ac7fe5aSThomas Gleixner }
12473ac7fe5aSThomas Gleixner 
1248aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test = {
12493ac7fe5aSThomas Gleixner 	.name			= "selftest",
1250b9fdac7fSDu, Changbin 	.is_static_object	= is_static_object,
12513ac7fe5aSThomas Gleixner 	.fixup_init		= fixup_init,
12523ac7fe5aSThomas Gleixner 	.fixup_activate		= fixup_activate,
12533ac7fe5aSThomas Gleixner 	.fixup_destroy		= fixup_destroy,
12543ac7fe5aSThomas Gleixner 	.fixup_free		= fixup_free,
12553ac7fe5aSThomas Gleixner };
12563ac7fe5aSThomas Gleixner 
12573ac7fe5aSThomas Gleixner static __initdata struct self_test obj = { .static_init = 0 };
12583ac7fe5aSThomas Gleixner 
125955fb412eSThomas Gleixner static bool __init debug_objects_selftest(void)
12603ac7fe5aSThomas Gleixner {
12613ac7fe5aSThomas Gleixner 	int fixups, oldfixups, warnings, oldwarnings;
12623ac7fe5aSThomas Gleixner 	unsigned long flags;
12633ac7fe5aSThomas Gleixner 
12643ac7fe5aSThomas Gleixner 	local_irq_save(flags);
12653ac7fe5aSThomas Gleixner 
12663ac7fe5aSThomas Gleixner 	fixups = oldfixups = debug_objects_fixups;
12673ac7fe5aSThomas Gleixner 	warnings = oldwarnings = debug_objects_warnings;
12683ac7fe5aSThomas Gleixner 	descr_test = &descr_type_test;
12693ac7fe5aSThomas Gleixner 
12703ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12713ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
12723ac7fe5aSThomas Gleixner 		goto out;
12733ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12743ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
12753ac7fe5aSThomas Gleixner 		goto out;
12763ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12773ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
12783ac7fe5aSThomas Gleixner 		goto out;
12793ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
12803ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
12813ac7fe5aSThomas Gleixner 		goto out;
12823ac7fe5aSThomas Gleixner 	debug_object_destroy(&obj, &descr_type_test);
12833ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
12843ac7fe5aSThomas Gleixner 		goto out;
12853ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12863ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12873ac7fe5aSThomas Gleixner 		goto out;
12883ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12893ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12903ac7fe5aSThomas Gleixner 		goto out;
12913ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
12923ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12933ac7fe5aSThomas Gleixner 		goto out;
12943ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
12953ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
12963ac7fe5aSThomas Gleixner 		goto out;
12973ac7fe5aSThomas Gleixner 
12983ac7fe5aSThomas Gleixner 	obj.static_init = 1;
12993ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13009f78ff00SStephen Boyd 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
13013ac7fe5aSThomas Gleixner 		goto out;
13023ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
13033ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
13043ac7fe5aSThomas Gleixner 		goto out;
13053ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
13063ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
13073ac7fe5aSThomas Gleixner 		goto out;
13083ac7fe5aSThomas Gleixner 
13093ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
13103ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
13113ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
13123ac7fe5aSThomas Gleixner 		goto out;
13133ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13143ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
13153ac7fe5aSThomas Gleixner 		goto out;
13163ac7fe5aSThomas Gleixner 	__debug_check_no_obj_freed(&obj, sizeof(obj));
13173ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
13183ac7fe5aSThomas Gleixner 		goto out;
13193ac7fe5aSThomas Gleixner #endif
1320719e4843SFabian Frederick 	pr_info("selftest passed\n");
13213ac7fe5aSThomas Gleixner 
13223ac7fe5aSThomas Gleixner out:
13233ac7fe5aSThomas Gleixner 	debug_objects_fixups = oldfixups;
13243ac7fe5aSThomas Gleixner 	debug_objects_warnings = oldwarnings;
13253ac7fe5aSThomas Gleixner 	descr_test = NULL;
13263ac7fe5aSThomas Gleixner 
13273ac7fe5aSThomas Gleixner 	local_irq_restore(flags);
1328661cc28bSThomas Gleixner 	return debug_objects_enabled;
13293ac7fe5aSThomas Gleixner }
13303ac7fe5aSThomas Gleixner #else
133155fb412eSThomas Gleixner static inline bool debug_objects_selftest(void) { return true; }
13323ac7fe5aSThomas Gleixner #endif
13333ac7fe5aSThomas Gleixner 
13343ac7fe5aSThomas Gleixner /*
13353ac7fe5aSThomas Gleixner  * Called during early boot to initialize the hash buckets and link
13363ac7fe5aSThomas Gleixner  * the static object pool objects into the poll list. After this call
13373ac7fe5aSThomas Gleixner  * the object tracker is fully operational.
13383ac7fe5aSThomas Gleixner  */
13393ac7fe5aSThomas Gleixner void __init debug_objects_early_init(void)
13403ac7fe5aSThomas Gleixner {
13413ac7fe5aSThomas Gleixner 	int i;
13423ac7fe5aSThomas Gleixner 
13433ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1344aef9cb05SThomas Gleixner 		raw_spin_lock_init(&obj_hash[i].lock);
13453ac7fe5aSThomas Gleixner 
1346cb58d190SThomas Gleixner 	/* Keep early boot simple and add everything to the boot list */
13473ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1348cb58d190SThomas Gleixner 		hlist_add_head(&obj_static_pool[i].node, &pool_boot);
13493ac7fe5aSThomas Gleixner }
13503ac7fe5aSThomas Gleixner 
13513ac7fe5aSThomas Gleixner /*
135255fb412eSThomas Gleixner  * Convert the statically allocated objects to dynamic ones.
135355fb412eSThomas Gleixner  * debug_objects_mem_init() is called early so only one CPU is up and
135455fb412eSThomas Gleixner  * interrupts are disabled, which means it is safe to replace the active
135555fb412eSThomas Gleixner  * object references.
13561be1cb7bSThomas Gleixner  */
135755fb412eSThomas Gleixner static bool __init debug_objects_replace_static_objects(struct kmem_cache *cache)
13581be1cb7bSThomas Gleixner {
13591be1cb7bSThomas Gleixner 	struct debug_bucket *db = obj_hash;
13601be1cb7bSThomas Gleixner 	struct debug_obj *obj, *new;
136155fb412eSThomas Gleixner 	struct hlist_node *tmp;
13621be1cb7bSThomas Gleixner 	HLIST_HEAD(objects);
1363241463f4SThomas Gleixner 	int i;
13641be1cb7bSThomas Gleixner 
13651be1cb7bSThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
136655fb412eSThomas Gleixner 		obj = kmem_cache_zalloc(cache, GFP_KERNEL);
13671be1cb7bSThomas Gleixner 		if (!obj)
13681be1cb7bSThomas Gleixner 			goto free;
13691be1cb7bSThomas Gleixner 		hlist_add_head(&obj->node, &objects);
13701be1cb7bSThomas Gleixner 	}
13711be1cb7bSThomas Gleixner 
1372e18328ffSThomas Gleixner 	debug_objects_allocated = ODEBUG_POOL_SIZE;
1373e18328ffSThomas Gleixner 	pool_global.cnt = ODEBUG_POOL_SIZE;
1374eabb7f1aSwuchi 
13751be1cb7bSThomas Gleixner 	/*
1376cb58d190SThomas Gleixner 	 * Move the allocated objects to the global pool and disconnect the
1377cb58d190SThomas Gleixner 	 * boot pool.
1378a0ae9504SZhen Lei 	 */
1379e18328ffSThomas Gleixner 	hlist_move_list(&objects, &pool_global.objects);
1380cb58d190SThomas Gleixner 	pool_boot.first = NULL;
13811be1cb7bSThomas Gleixner 
13821be1cb7bSThomas Gleixner 	/* Replace the active object references */
13831be1cb7bSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
13841be1cb7bSThomas Gleixner 		hlist_move_list(&db->list, &objects);
13851be1cb7bSThomas Gleixner 
1386b67bfe0dSSasha Levin 		hlist_for_each_entry(obj, &objects, node) {
1387e18328ffSThomas Gleixner 			new = hlist_entry(pool_global.objects.first, typeof(*obj), node);
13881be1cb7bSThomas Gleixner 			hlist_del(&new->node);
1389e18328ffSThomas Gleixner 			pool_global.cnt--;
13901be1cb7bSThomas Gleixner 			/* copy object data */
13911be1cb7bSThomas Gleixner 			*new = *obj;
13921be1cb7bSThomas Gleixner 			hlist_add_head(&new->node, &db->list);
13931be1cb7bSThomas Gleixner 		}
13941be1cb7bSThomas Gleixner 	}
139555fb412eSThomas Gleixner 	return true;
13961be1cb7bSThomas Gleixner free:
139749a5cb82SThomas Gleixner 	/* Can't use free_object_list() as the cache is not populated yet */
1398b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(obj, tmp, &objects, node) {
13991be1cb7bSThomas Gleixner 		hlist_del(&obj->node);
140055fb412eSThomas Gleixner 		kmem_cache_free(cache, obj);
14011be1cb7bSThomas Gleixner 	}
140255fb412eSThomas Gleixner 	return false;
14031be1cb7bSThomas Gleixner }
14041be1cb7bSThomas Gleixner 
14051be1cb7bSThomas Gleixner /*
14063ac7fe5aSThomas Gleixner  * Called after the kmem_caches are functional to setup a dedicated
14073ac7fe5aSThomas Gleixner  * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
14083ac7fe5aSThomas Gleixner  * prevents that the debug code is called on kmem_cache_free() for the
14093ac7fe5aSThomas Gleixner  * debug tracker objects to avoid recursive calls.
14103ac7fe5aSThomas Gleixner  */
14113ac7fe5aSThomas Gleixner void __init debug_objects_mem_init(void)
14123ac7fe5aSThomas Gleixner {
141355fb412eSThomas Gleixner 	struct kmem_cache *cache;
14143f397bf9SThomas Gleixner 	int extras;
1415d86998b1SWaiman Long 
14163ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
14173ac7fe5aSThomas Gleixner 		return;
14183ac7fe5aSThomas Gleixner 
141955fb412eSThomas Gleixner 	if (!debug_objects_selftest())
1420eabb7f1aSwuchi 		return;
142155fb412eSThomas Gleixner 
142255fb412eSThomas Gleixner 	cache = kmem_cache_create("debug_objects_cache", sizeof (struct debug_obj), 0,
142355fb412eSThomas Gleixner 				  SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE, NULL);
142455fb412eSThomas Gleixner 
142555fb412eSThomas Gleixner 	if (!cache || !debug_objects_replace_static_objects(cache)) {
1426661cc28bSThomas Gleixner 		debug_objects_enabled = false;
142755fb412eSThomas Gleixner 		pr_warn("Out of memory.\n");
142855fb412eSThomas Gleixner 		return;
142955fb412eSThomas Gleixner 	}
143055fb412eSThomas Gleixner 
143155fb412eSThomas Gleixner 	/*
143255fb412eSThomas Gleixner 	 * Adjust the thresholds for allocating and freeing objects
143355fb412eSThomas Gleixner 	 * according to the number of possible CPUs available in the
143455fb412eSThomas Gleixner 	 * system.
143555fb412eSThomas Gleixner 	 */
143655fb412eSThomas Gleixner 	extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
1437*96a9a042SThomas Gleixner 	pool_global.max_cnt += extras;
1438*96a9a042SThomas Gleixner 	pool_global.min_cnt += extras;
143955fb412eSThomas Gleixner 
144055fb412eSThomas Gleixner 	/* Everything worked. Expose the cache */
144155fb412eSThomas Gleixner 	obj_cache = cache;
1442634d61f4SWaiman Long 
144388451f2cSZqiang #ifdef CONFIG_HOTPLUG_CPU
144488451f2cSZqiang 	cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL,
144588451f2cSZqiang 				  object_cpu_offline);
144688451f2cSZqiang #endif
144755fb412eSThomas Gleixner 	return;
14483ac7fe5aSThomas Gleixner }
1449