xref: /linux-6.15/lib/debugobjects.c (revision cb58d190)
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 
46d86998b1SWaiman Long /*
47d86998b1SWaiman Long  * Debug object percpu free list
48d86998b1SWaiman Long  * Access is protected by disabling irq
49d86998b1SWaiman Long  */
50d86998b1SWaiman Long struct debug_percpu_free {
51d86998b1SWaiman Long 	struct hlist_head	free_objs;
52d86998b1SWaiman Long 	int			obj_free;
53d86998b1SWaiman Long };
54d86998b1SWaiman Long 
55e18328ffSThomas Gleixner struct obj_pool {
56e18328ffSThomas Gleixner 	struct hlist_head	objects;
57e18328ffSThomas Gleixner 	unsigned int		cnt;
58e18328ffSThomas Gleixner } ____cacheline_aligned;
59e18328ffSThomas Gleixner 
60d86998b1SWaiman Long static DEFINE_PER_CPU(struct debug_percpu_free, percpu_obj_pool);
61d86998b1SWaiman Long 
623ac7fe5aSThomas Gleixner static struct debug_bucket	obj_hash[ODEBUG_HASH_SIZE];
633ac7fe5aSThomas Gleixner 
641be1cb7bSThomas Gleixner static struct debug_obj		obj_static_pool[ODEBUG_POOL_SIZE] __initdata;
653ac7fe5aSThomas Gleixner 
66aef9cb05SThomas Gleixner static DEFINE_RAW_SPINLOCK(pool_lock);
673ac7fe5aSThomas Gleixner 
68e18328ffSThomas Gleixner static struct obj_pool		pool_global;
69e18328ffSThomas Gleixner static struct obj_pool		pool_to_free;
703ac7fe5aSThomas Gleixner 
71*cb58d190SThomas Gleixner static HLIST_HEAD(pool_boot);
72*cb58d190SThomas Gleixner 
73d86998b1SWaiman Long /*
74d86998b1SWaiman Long  * Because of the presence of percpu free pools, obj_pool_free will
75d86998b1SWaiman Long  * under-count those in the percpu free pools. Similarly, obj_pool_used
76d86998b1SWaiman Long  * will over-count those in the percpu free pools. Adjustments will be
77d86998b1SWaiman Long  * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
78d86998b1SWaiman Long  * can be off.
79d86998b1SWaiman Long  */
80e4757c71SZhen Lei static int __data_racy		obj_pool_min_free = ODEBUG_POOL_SIZE;
813ac7fe5aSThomas Gleixner static int			obj_pool_used;
82e4757c71SZhen Lei static int __data_racy		obj_pool_max_used;
83a7344a68SWaiman Long static bool			obj_freeing;
843ac7fe5aSThomas Gleixner 
855b5baba6SBreno Leitao static int __data_racy			debug_objects_maxchain __read_mostly;
865b5baba6SBreno Leitao static int __data_racy __maybe_unused	debug_objects_maxchecked __read_mostly;
875b5baba6SBreno Leitao static int __data_racy			debug_objects_fixups __read_mostly;
885b5baba6SBreno Leitao static int __data_racy			debug_objects_warnings __read_mostly;
89661cc28bSThomas Gleixner static bool __data_racy			debug_objects_enabled __read_mostly
903ae70205SIngo Molnar 					= CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
91e4757c71SZhen Lei static int				debug_objects_pool_size __ro_after_init
9297dd552eSWaiman Long 					= ODEBUG_POOL_SIZE;
93e4757c71SZhen Lei static int				debug_objects_pool_min_level __ro_after_init
9497dd552eSWaiman Long 					= ODEBUG_POOL_MIN_LEVEL;
955b5baba6SBreno Leitao 
96aedcade6SStephen Boyd static const struct debug_obj_descr *descr_test  __read_mostly;
9768279f9cSAlexey Dobriyan static struct kmem_cache	*obj_cache __ro_after_init;
983ac7fe5aSThomas Gleixner 
99c4b73aabSWaiman Long /*
1000cad93c3SWaiman Long  * Track numbers of kmem_cache_alloc()/free() calls done.
101c4b73aabSWaiman Long  */
102e4757c71SZhen Lei static int __data_racy		debug_objects_allocated;
103e4757c71SZhen Lei static int __data_racy		debug_objects_freed;
104c4b73aabSWaiman Long 
105337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work);
106a7344a68SWaiman Long static DECLARE_DELAYED_WORK(debug_obj_work, free_obj_work);
107337fff8bSThomas Gleixner 
1083ac7fe5aSThomas Gleixner static int __init enable_object_debug(char *str)
1093ac7fe5aSThomas Gleixner {
110661cc28bSThomas Gleixner 	debug_objects_enabled = true;
1113ac7fe5aSThomas Gleixner 	return 0;
1123ac7fe5aSThomas Gleixner }
113661cc28bSThomas Gleixner early_param("debug_objects", enable_object_debug);
1143e8ebb5cSKyle McMartin 
1153e8ebb5cSKyle McMartin static int __init disable_object_debug(char *str)
1163e8ebb5cSKyle McMartin {
117661cc28bSThomas Gleixner 	debug_objects_enabled = false;
1183e8ebb5cSKyle McMartin 	return 0;
1193e8ebb5cSKyle McMartin }
1203e8ebb5cSKyle McMartin early_param("no_debug_objects", disable_object_debug);
1213ac7fe5aSThomas Gleixner 
1223ac7fe5aSThomas Gleixner static const char *obj_states[ODEBUG_STATE_MAX] = {
1233ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_NONE]		= "none",
1243ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_INIT]		= "initialized",
1253ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_INACTIVE]		= "inactive",
1263ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_ACTIVE]		= "active",
1273ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_DESTROYED]	= "destroyed",
1283ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_NOTAVAILABLE]	= "not available",
1293ac7fe5aSThomas Gleixner };
1303ac7fe5aSThomas Gleixner 
131e18328ffSThomas Gleixner static __always_inline unsigned int pool_count(struct obj_pool *pool)
132e18328ffSThomas Gleixner {
133e18328ffSThomas Gleixner 	return READ_ONCE(pool->cnt);
134e18328ffSThomas Gleixner }
135e18328ffSThomas Gleixner 
136e18328ffSThomas Gleixner static inline bool pool_global_should_refill(void)
137e18328ffSThomas Gleixner {
138e18328ffSThomas Gleixner 	return READ_ONCE(pool_global.cnt) < debug_objects_pool_min_level;
139e18328ffSThomas Gleixner }
140e18328ffSThomas Gleixner 
141e18328ffSThomas Gleixner static inline bool pool_global_must_refill(void)
142e18328ffSThomas Gleixner {
143e18328ffSThomas Gleixner 	return READ_ONCE(pool_global.cnt) < (debug_objects_pool_min_level / 2);
144e18328ffSThomas Gleixner }
145e18328ffSThomas Gleixner 
14649a5cb82SThomas Gleixner static void free_object_list(struct hlist_head *head)
14749a5cb82SThomas Gleixner {
14849a5cb82SThomas Gleixner 	struct hlist_node *tmp;
14949a5cb82SThomas Gleixner 	struct debug_obj *obj;
15049a5cb82SThomas Gleixner 	int cnt = 0;
15149a5cb82SThomas Gleixner 
15249a5cb82SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, head, node) {
15349a5cb82SThomas Gleixner 		hlist_del(&obj->node);
15449a5cb82SThomas Gleixner 		kmem_cache_free(obj_cache, obj);
15549a5cb82SThomas Gleixner 		cnt++;
15649a5cb82SThomas Gleixner 	}
15749a5cb82SThomas Gleixner 	debug_objects_freed += cnt;
15849a5cb82SThomas Gleixner }
15949a5cb82SThomas Gleixner 
160d8c6cd3aSZhen Lei static void fill_pool_from_freelist(void)
1613ac7fe5aSThomas Gleixner {
162d8c6cd3aSZhen Lei 	static unsigned long state;
163d26bf505SWaiman Long 	struct debug_obj *obj;
1643ac7fe5aSThomas Gleixner 
16536c4ead6SYang Shi 	/*
16663a4a9b5SZhen Lei 	 * Reuse objs from the global obj_to_free list; they will be
16763a4a9b5SZhen Lei 	 * reinitialized when allocating.
16836c4ead6SYang Shi 	 */
169e18328ffSThomas Gleixner 	if (!pool_count(&pool_to_free))
170d8c6cd3aSZhen Lei 		return;
171d8c6cd3aSZhen Lei 
172d8c6cd3aSZhen Lei 	/*
173d8c6cd3aSZhen Lei 	 * Prevent the context from being scheduled or interrupted after
174d8c6cd3aSZhen Lei 	 * setting the state flag;
175d8c6cd3aSZhen Lei 	 */
176d8c6cd3aSZhen Lei 	guard(irqsave)();
177d8c6cd3aSZhen Lei 
178d8c6cd3aSZhen Lei 	/*
179d8c6cd3aSZhen Lei 	 * Avoid lock contention on &pool_lock and avoid making the cache
180d8c6cd3aSZhen Lei 	 * line exclusive by testing the bit before attempting to set it.
181d8c6cd3aSZhen Lei 	 */
182d8c6cd3aSZhen Lei 	if (test_bit(0, &state) || test_and_set_bit(0, &state))
183d8c6cd3aSZhen Lei 		return;
184d8c6cd3aSZhen Lei 
185d8c6cd3aSZhen Lei 	guard(raw_spinlock)(&pool_lock);
18636c4ead6SYang Shi 	/*
18736c4ead6SYang Shi 	 * Recheck with the lock held as the worker thread might have
18836c4ead6SYang Shi 	 * won the race and freed the global free list already.
18936c4ead6SYang Shi 	 */
190e18328ffSThomas Gleixner 	while (pool_to_free.cnt && (pool_global.cnt < debug_objects_pool_min_level)) {
191e18328ffSThomas Gleixner 		obj = hlist_entry(pool_to_free.objects.first, typeof(*obj), node);
19236c4ead6SYang Shi 		hlist_del(&obj->node);
193e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt - 1);
194e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_global.objects);
195e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + 1);
19636c4ead6SYang Shi 	}
197d8c6cd3aSZhen Lei 	clear_bit(0, &state);
19836c4ead6SYang Shi }
19936c4ead6SYang Shi 
200d8c6cd3aSZhen Lei static void fill_pool(void)
201d8c6cd3aSZhen Lei {
202d8c6cd3aSZhen Lei 	static atomic_t cpus_allocating;
203d8c6cd3aSZhen Lei 
204d8c6cd3aSZhen Lei 	/*
205d8c6cd3aSZhen Lei 	 * Avoid allocation and lock contention when:
206d8c6cd3aSZhen Lei 	 *   - One other CPU is already allocating
207d8c6cd3aSZhen Lei 	 *   - the global pool has not reached the critical level yet
208d8c6cd3aSZhen Lei 	 */
209e18328ffSThomas Gleixner 	if (!pool_global_must_refill() && atomic_read(&cpus_allocating))
2101fda107dSThomas Gleixner 		return;
2113ac7fe5aSThomas Gleixner 
212d8c6cd3aSZhen Lei 	atomic_inc(&cpus_allocating);
213e18328ffSThomas Gleixner 	while (pool_global_should_refill()) {
214813fd078SZhen Lei 		struct debug_obj *new, *last = NULL;
215813fd078SZhen Lei 		HLIST_HEAD(head);
216d26bf505SWaiman Long 		int cnt;
2173ac7fe5aSThomas Gleixner 
218d26bf505SWaiman Long 		for (cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) {
219d8c6cd3aSZhen Lei 			new = kmem_cache_zalloc(obj_cache, __GFP_HIGH | __GFP_NOWARN);
220813fd078SZhen Lei 			if (!new)
221d26bf505SWaiman Long 				break;
222813fd078SZhen Lei 			hlist_add_head(&new->node, &head);
223813fd078SZhen Lei 			if (!last)
224813fd078SZhen Lei 				last = new;
225d26bf505SWaiman Long 		}
226d26bf505SWaiman Long 		if (!cnt)
227d8c6cd3aSZhen Lei 			break;
2283ac7fe5aSThomas Gleixner 
229d8c6cd3aSZhen Lei 		guard(raw_spinlock_irqsave)(&pool_lock);
230e18328ffSThomas Gleixner 		hlist_splice_init(&head, &last->node, &pool_global.objects);
231813fd078SZhen Lei 		debug_objects_allocated += cnt;
232e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + cnt);
2333ac7fe5aSThomas Gleixner 	}
234d8c6cd3aSZhen Lei 	atomic_dec(&cpus_allocating);
2353ac7fe5aSThomas Gleixner }
2363ac7fe5aSThomas Gleixner 
2373ac7fe5aSThomas Gleixner /*
2383ac7fe5aSThomas Gleixner  * Lookup an object in the hash bucket.
2393ac7fe5aSThomas Gleixner  */
2403ac7fe5aSThomas Gleixner static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
2413ac7fe5aSThomas Gleixner {
2423ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
2433ac7fe5aSThomas Gleixner 	int cnt = 0;
2443ac7fe5aSThomas Gleixner 
245b67bfe0dSSasha Levin 	hlist_for_each_entry(obj, &b->list, node) {
2463ac7fe5aSThomas Gleixner 		cnt++;
2473ac7fe5aSThomas Gleixner 		if (obj->object == addr)
2483ac7fe5aSThomas Gleixner 			return obj;
2493ac7fe5aSThomas Gleixner 	}
2503ac7fe5aSThomas Gleixner 	if (cnt > debug_objects_maxchain)
2513ac7fe5aSThomas Gleixner 		debug_objects_maxchain = cnt;
2523ac7fe5aSThomas Gleixner 
2533ac7fe5aSThomas Gleixner 	return NULL;
2543ac7fe5aSThomas Gleixner }
2553ac7fe5aSThomas Gleixner 
2563ac7fe5aSThomas Gleixner /*
257d86998b1SWaiman Long  * Allocate a new object from the hlist
258d86998b1SWaiman Long  */
259d86998b1SWaiman Long static struct debug_obj *__alloc_object(struct hlist_head *list)
260d86998b1SWaiman Long {
261d86998b1SWaiman Long 	struct debug_obj *obj = NULL;
262d86998b1SWaiman Long 
263d86998b1SWaiman Long 	if (list->first) {
264d86998b1SWaiman Long 		obj = hlist_entry(list->first, typeof(*obj), node);
265d86998b1SWaiman Long 		hlist_del(&obj->node);
266d86998b1SWaiman Long 	}
267d86998b1SWaiman Long 
268d86998b1SWaiman Long 	return obj;
269d86998b1SWaiman Long }
270d86998b1SWaiman Long 
2713ac7fe5aSThomas Gleixner static struct debug_obj *
272aedcade6SStephen Boyd alloc_object(void *addr, struct debug_bucket *b, const struct debug_obj_descr *descr)
2733ac7fe5aSThomas Gleixner {
274634d61f4SWaiman Long 	struct debug_percpu_free *percpu_pool = this_cpu_ptr(&percpu_obj_pool);
275d86998b1SWaiman Long 	struct debug_obj *obj;
276d86998b1SWaiman Long 
277d86998b1SWaiman Long 	if (likely(obj_cache)) {
278d86998b1SWaiman Long 		obj = __alloc_object(&percpu_pool->free_objs);
279d86998b1SWaiman Long 		if (obj) {
280d86998b1SWaiman Long 			percpu_pool->obj_free--;
281d86998b1SWaiman Long 			goto init_obj;
282d86998b1SWaiman Long 		}
283*cb58d190SThomas Gleixner 	} else {
284*cb58d190SThomas Gleixner 		obj = __alloc_object(&pool_boot);
285*cb58d190SThomas Gleixner 		goto init_obj;
286d86998b1SWaiman Long 	}
2873ac7fe5aSThomas Gleixner 
288aef9cb05SThomas Gleixner 	raw_spin_lock(&pool_lock);
289e18328ffSThomas Gleixner 	obj = __alloc_object(&pool_global.objects);
290d86998b1SWaiman Long 	if (obj) {
2913ac7fe5aSThomas Gleixner 		obj_pool_used++;
292e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt - 1);
293634d61f4SWaiman Long 
294634d61f4SWaiman Long 		/*
295634d61f4SWaiman Long 		 * Looking ahead, allocate one batch of debug objects and
296634d61f4SWaiman Long 		 * put them into the percpu free pool.
297634d61f4SWaiman Long 		 */
298634d61f4SWaiman Long 		if (likely(obj_cache)) {
299634d61f4SWaiman Long 			int i;
300634d61f4SWaiman Long 
301634d61f4SWaiman Long 			for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
302634d61f4SWaiman Long 				struct debug_obj *obj2;
303634d61f4SWaiman Long 
304e18328ffSThomas Gleixner 				obj2 = __alloc_object(&pool_global.objects);
305634d61f4SWaiman Long 				if (!obj2)
306634d61f4SWaiman Long 					break;
307e18328ffSThomas Gleixner 				hlist_add_head(&obj2->node, &percpu_pool->free_objs);
308634d61f4SWaiman Long 				percpu_pool->obj_free++;
309634d61f4SWaiman Long 				obj_pool_used++;
310e18328ffSThomas Gleixner 				WRITE_ONCE(pool_global.cnt, pool_global.cnt - 1);
311634d61f4SWaiman Long 			}
312634d61f4SWaiman Long 		}
313634d61f4SWaiman Long 
3143ac7fe5aSThomas Gleixner 		if (obj_pool_used > obj_pool_max_used)
3153ac7fe5aSThomas Gleixner 			obj_pool_max_used = obj_pool_used;
3163ac7fe5aSThomas Gleixner 
317e18328ffSThomas Gleixner 		if (pool_global.cnt < obj_pool_min_free)
318e18328ffSThomas Gleixner 			obj_pool_min_free = pool_global.cnt;
3193ac7fe5aSThomas Gleixner 	}
320aef9cb05SThomas Gleixner 	raw_spin_unlock(&pool_lock);
3213ac7fe5aSThomas Gleixner 
322d86998b1SWaiman Long init_obj:
323d86998b1SWaiman Long 	if (obj) {
324d86998b1SWaiman Long 		obj->object = addr;
325d86998b1SWaiman Long 		obj->descr  = descr;
326d86998b1SWaiman Long 		obj->state  = ODEBUG_STATE_NONE;
327d86998b1SWaiman Long 		obj->astate = 0;
328d86998b1SWaiman Long 		hlist_add_head(&obj->node, &b->list);
329d86998b1SWaiman Long 	}
3303ac7fe5aSThomas Gleixner 	return obj;
3313ac7fe5aSThomas Gleixner }
3323ac7fe5aSThomas Gleixner 
3333ac7fe5aSThomas Gleixner /*
334337fff8bSThomas Gleixner  * workqueue function to free objects.
335858274b6SWaiman Long  *
336858274b6SWaiman Long  * To reduce contention on the global pool_lock, the actual freeing of
337636e1970SYang Shi  * debug objects will be delayed if the pool_lock is busy.
338337fff8bSThomas Gleixner  */
339337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work)
340337fff8bSThomas Gleixner {
34136c4ead6SYang Shi 	struct debug_obj *obj;
342337fff8bSThomas Gleixner 	unsigned long flags;
34336c4ead6SYang Shi 	HLIST_HEAD(tofree);
344337fff8bSThomas Gleixner 
345a7344a68SWaiman Long 	WRITE_ONCE(obj_freeing, false);
346858274b6SWaiman Long 	if (!raw_spin_trylock_irqsave(&pool_lock, flags))
347858274b6SWaiman Long 		return;
34836c4ead6SYang Shi 
349e18328ffSThomas Gleixner 	if (pool_global.cnt >= debug_objects_pool_size)
350a7344a68SWaiman Long 		goto free_objs;
351a7344a68SWaiman Long 
35236c4ead6SYang Shi 	/*
35336c4ead6SYang Shi 	 * The objs on the pool list might be allocated before the work is
35436c4ead6SYang Shi 	 * run, so recheck if pool list it full or not, if not fill pool
355a7344a68SWaiman Long 	 * list from the global free list. As it is likely that a workload
356a7344a68SWaiman Long 	 * may be gearing up to use more and more objects, don't free any
357a7344a68SWaiman Long 	 * of them until the next round.
35836c4ead6SYang Shi 	 */
359e18328ffSThomas Gleixner 	while (pool_to_free.cnt && pool_global.cnt < debug_objects_pool_size) {
360e18328ffSThomas Gleixner 		obj = hlist_entry(pool_to_free.objects.first, typeof(*obj), node);
36136c4ead6SYang Shi 		hlist_del(&obj->node);
362e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_global.objects);
363e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt - 1);
364e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + 1);
36536c4ead6SYang Shi 	}
366a7344a68SWaiman Long 	raw_spin_unlock_irqrestore(&pool_lock, flags);
367a7344a68SWaiman Long 	return;
36836c4ead6SYang Shi 
369a7344a68SWaiman Long free_objs:
37036c4ead6SYang Shi 	/*
37136c4ead6SYang Shi 	 * Pool list is already full and there are still objs on the free
37236c4ead6SYang Shi 	 * list. Move remaining free objs to a temporary list to free the
37336c4ead6SYang Shi 	 * memory outside the pool_lock held region.
37436c4ead6SYang Shi 	 */
375e18328ffSThomas Gleixner 	if (pool_to_free.cnt) {
376e18328ffSThomas Gleixner 		hlist_move_list(&pool_to_free.objects, &tofree);
377e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, 0);
37836c4ead6SYang Shi 	}
379aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&pool_lock, flags);
38036c4ead6SYang Shi 
38149a5cb82SThomas Gleixner 	free_object_list(&tofree);
382337fff8bSThomas Gleixner }
383337fff8bSThomas Gleixner 
384a7344a68SWaiman Long static void __free_object(struct debug_obj *obj)
385636e1970SYang Shi {
386634d61f4SWaiman Long 	struct debug_obj *objs[ODEBUG_BATCH_SIZE];
387634d61f4SWaiman Long 	struct debug_percpu_free *percpu_pool;
388634d61f4SWaiman Long 	int lookahead_count = 0;
389636e1970SYang Shi 	bool work;
390636e1970SYang Shi 
391*cb58d190SThomas Gleixner 	guard(irqsave)();
392*cb58d190SThomas Gleixner 
393*cb58d190SThomas Gleixner 	if (unlikely(!obj_cache)) {
394*cb58d190SThomas Gleixner 		hlist_add_head(&obj->node, &pool_boot);
395*cb58d190SThomas Gleixner 		return;
396*cb58d190SThomas Gleixner 	}
397634d61f4SWaiman Long 
398d86998b1SWaiman Long 	/*
399d86998b1SWaiman Long 	 * Try to free it into the percpu pool first.
400d86998b1SWaiman Long 	 */
401d86998b1SWaiman Long 	percpu_pool = this_cpu_ptr(&percpu_obj_pool);
402634d61f4SWaiman Long 	if (percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) {
403d86998b1SWaiman Long 		hlist_add_head(&obj->node, &percpu_pool->free_objs);
404d86998b1SWaiman Long 		percpu_pool->obj_free++;
405a7344a68SWaiman Long 		return;
406d86998b1SWaiman Long 	}
407d86998b1SWaiman Long 
408634d61f4SWaiman Long 	/*
409634d61f4SWaiman Long 	 * As the percpu pool is full, look ahead and pull out a batch
410634d61f4SWaiman Long 	 * of objects from the percpu pool and free them as well.
411634d61f4SWaiman Long 	 */
412634d61f4SWaiman Long 	for (; lookahead_count < ODEBUG_BATCH_SIZE; lookahead_count++) {
413634d61f4SWaiman Long 		objs[lookahead_count] = __alloc_object(&percpu_pool->free_objs);
414634d61f4SWaiman Long 		if (!objs[lookahead_count])
415634d61f4SWaiman Long 			break;
416634d61f4SWaiman Long 		percpu_pool->obj_free--;
417634d61f4SWaiman Long 	}
418634d61f4SWaiman Long 
419d86998b1SWaiman Long 	raw_spin_lock(&pool_lock);
420e18328ffSThomas Gleixner 	work = (pool_global.cnt > debug_objects_pool_size) && obj_cache &&
421e18328ffSThomas Gleixner 	       (pool_to_free.cnt < ODEBUG_FREE_WORK_MAX);
422636e1970SYang Shi 	obj_pool_used--;
423636e1970SYang Shi 
424636e1970SYang Shi 	if (work) {
425e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt + 1);
426e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_to_free.objects);
427634d61f4SWaiman Long 		if (lookahead_count) {
428e18328ffSThomas Gleixner 			WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt + lookahead_count);
429634d61f4SWaiman Long 			obj_pool_used -= lookahead_count;
430634d61f4SWaiman Long 			while (lookahead_count) {
431634d61f4SWaiman Long 				hlist_add_head(&objs[--lookahead_count]->node,
432e18328ffSThomas Gleixner 					       &pool_to_free.objects);
433634d61f4SWaiman Long 			}
434634d61f4SWaiman Long 		}
435a7344a68SWaiman Long 
436e18328ffSThomas Gleixner 		if ((pool_global.cnt > debug_objects_pool_size) &&
437e18328ffSThomas Gleixner 		    (pool_to_free.cnt < ODEBUG_FREE_WORK_MAX)) {
438a7344a68SWaiman Long 			int i;
439a7344a68SWaiman Long 
440a7344a68SWaiman Long 			/*
441a7344a68SWaiman Long 			 * Free one more batch of objects from obj_pool.
442a7344a68SWaiman Long 			 */
443a7344a68SWaiman Long 			for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
444e18328ffSThomas Gleixner 				obj = __alloc_object(&pool_global.objects);
445e18328ffSThomas Gleixner 				hlist_add_head(&obj->node, &pool_to_free.objects);
446e18328ffSThomas Gleixner 				WRITE_ONCE(pool_global.cnt, pool_global.cnt - 1);
447e18328ffSThomas Gleixner 				WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt + 1);
448a7344a68SWaiman Long 			}
449a7344a68SWaiman Long 		}
450636e1970SYang Shi 	} else {
451e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + 1);
452e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_global.objects);
453634d61f4SWaiman Long 		if (lookahead_count) {
454e18328ffSThomas Gleixner 			WRITE_ONCE(pool_global.cnt, pool_global.cnt + lookahead_count);
455634d61f4SWaiman Long 			obj_pool_used -= lookahead_count;
456634d61f4SWaiman Long 			while (lookahead_count) {
457634d61f4SWaiman Long 				hlist_add_head(&objs[--lookahead_count]->node,
458e18328ffSThomas Gleixner 					       &pool_global.objects);
459634d61f4SWaiman Long 			}
460634d61f4SWaiman Long 		}
461636e1970SYang Shi 	}
462d86998b1SWaiman Long 	raw_spin_unlock(&pool_lock);
463636e1970SYang Shi }
464636e1970SYang Shi 
465337fff8bSThomas Gleixner /*
466337fff8bSThomas Gleixner  * Put the object back into the pool and schedule work to free objects
467337fff8bSThomas Gleixner  * if necessary.
4683ac7fe5aSThomas Gleixner  */
4693ac7fe5aSThomas Gleixner static void free_object(struct debug_obj *obj)
4703ac7fe5aSThomas Gleixner {
471a7344a68SWaiman Long 	__free_object(obj);
472e18328ffSThomas Gleixner 	if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
473a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
474a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
475a7344a68SWaiman Long 	}
4763ac7fe5aSThomas Gleixner }
4773ac7fe5aSThomas Gleixner 
478a2a70238SThomas Gleixner static void put_objects(struct hlist_head *list)
47988451f2cSZqiang {
48088451f2cSZqiang 	struct hlist_node *tmp;
48188451f2cSZqiang 	struct debug_obj *obj;
48288451f2cSZqiang 
483a2a70238SThomas Gleixner 	/*
484a2a70238SThomas Gleixner 	 * Using free_object() puts the objects into reuse or schedules
485a2a70238SThomas Gleixner 	 * them for freeing and it get's all the accounting correct.
486a2a70238SThomas Gleixner 	 */
487a2a70238SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, list, node) {
48888451f2cSZqiang 		hlist_del(&obj->node);
489a2a70238SThomas Gleixner 		free_object(obj);
490a2a70238SThomas Gleixner 	}
49188451f2cSZqiang }
492eabb7f1aSwuchi 
49349968cf1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
494a2a70238SThomas Gleixner static int object_cpu_offline(unsigned int cpu)
495a2a70238SThomas Gleixner {
496a2a70238SThomas Gleixner 	/* Remote access is safe as the CPU is dead already */
497a2a70238SThomas Gleixner 	struct debug_percpu_free *pcp = per_cpu_ptr(&percpu_obj_pool, cpu);
498eabb7f1aSwuchi 
499a2a70238SThomas Gleixner 	put_objects(&pcp->free_objs);
500a2a70238SThomas Gleixner 	pcp->obj_free = 0;
50188451f2cSZqiang 	return 0;
50288451f2cSZqiang }
50388451f2cSZqiang #endif
50488451f2cSZqiang 
50549968cf1SThomas Gleixner /* Out of memory. Free all objects from hash */
5063ac7fe5aSThomas Gleixner static void debug_objects_oom(void)
5073ac7fe5aSThomas Gleixner {
5083ac7fe5aSThomas Gleixner 	struct debug_bucket *db = obj_hash;
509673d62ccSVegard Nossum 	HLIST_HEAD(freelist);
5103ac7fe5aSThomas Gleixner 
511719e4843SFabian Frederick 	pr_warn("Out of memory. ODEBUG disabled\n");
5123ac7fe5aSThomas Gleixner 
51349968cf1SThomas Gleixner 	for (int i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
51449968cf1SThomas Gleixner 		scoped_guard(raw_spinlock_irqsave, &db->lock)
515673d62ccSVegard Nossum 			hlist_move_list(&db->list, &freelist);
516673d62ccSVegard Nossum 
51749968cf1SThomas Gleixner 		put_objects(&freelist);
5183ac7fe5aSThomas Gleixner 	}
5193ac7fe5aSThomas Gleixner }
5203ac7fe5aSThomas Gleixner 
5213ac7fe5aSThomas Gleixner /*
5223ac7fe5aSThomas Gleixner  * We use the pfn of the address for the hash. That way we can check
5233ac7fe5aSThomas Gleixner  * for freed objects simply by checking the affected bucket.
5243ac7fe5aSThomas Gleixner  */
5253ac7fe5aSThomas Gleixner static struct debug_bucket *get_bucket(unsigned long addr)
5263ac7fe5aSThomas Gleixner {
5273ac7fe5aSThomas Gleixner 	unsigned long hash;
5283ac7fe5aSThomas Gleixner 
5293ac7fe5aSThomas Gleixner 	hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
5303ac7fe5aSThomas Gleixner 	return &obj_hash[hash];
5313ac7fe5aSThomas Gleixner }
5323ac7fe5aSThomas Gleixner 
5333ac7fe5aSThomas Gleixner static void debug_print_object(struct debug_obj *obj, char *msg)
5343ac7fe5aSThomas Gleixner {
535aedcade6SStephen Boyd 	const struct debug_obj_descr *descr = obj->descr;
5363ac7fe5aSThomas Gleixner 	static int limit;
5373ac7fe5aSThomas Gleixner 
5388b64d420STetsuo Handa 	/*
5398b64d420STetsuo Handa 	 * Don't report if lookup_object_or_alloc() by the current thread
5408b64d420STetsuo Handa 	 * failed because lookup_object_or_alloc()/debug_objects_oom() by a
5418b64d420STetsuo Handa 	 * concurrent thread turned off debug_objects_enabled and cleared
5428b64d420STetsuo Handa 	 * the hash buckets.
5438b64d420STetsuo Handa 	 */
5448b64d420STetsuo Handa 	if (!debug_objects_enabled)
5458b64d420STetsuo Handa 		return;
5468b64d420STetsuo Handa 
54799777288SStanislaw Gruszka 	if (limit < 5 && descr != descr_test) {
54899777288SStanislaw Gruszka 		void *hint = descr->debug_hint ?
54999777288SStanislaw Gruszka 			descr->debug_hint(obj->object) : NULL;
5503ac7fe5aSThomas Gleixner 		limit++;
551a5d8e467SMathieu Desnoyers 		WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
552c4db2d3bSStephen Boyd 				 "object: %p object type: %s hint: %pS\n",
553a5d8e467SMathieu Desnoyers 			msg, obj_states[obj->state], obj->astate,
554c4db2d3bSStephen Boyd 			obj->object, descr->name, hint);
5553ac7fe5aSThomas Gleixner 	}
5563ac7fe5aSThomas Gleixner 	debug_objects_warnings++;
5573ac7fe5aSThomas Gleixner }
5583ac7fe5aSThomas Gleixner 
5593ac7fe5aSThomas Gleixner /*
5603ac7fe5aSThomas Gleixner  * Try to repair the damage, so we have a better chance to get useful
5613ac7fe5aSThomas Gleixner  * debug output.
5623ac7fe5aSThomas Gleixner  */
563b1e4d9d8SDu, Changbin static bool
564b1e4d9d8SDu, Changbin debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
5653ac7fe5aSThomas Gleixner 		   void * addr, enum debug_obj_state state)
5663ac7fe5aSThomas Gleixner {
567b1e4d9d8SDu, Changbin 	if (fixup && fixup(addr, state)) {
568b1e4d9d8SDu, Changbin 		debug_objects_fixups++;
569b1e4d9d8SDu, Changbin 		return true;
570b1e4d9d8SDu, Changbin 	}
571b1e4d9d8SDu, Changbin 	return false;
5723ac7fe5aSThomas Gleixner }
5733ac7fe5aSThomas Gleixner 
5743ac7fe5aSThomas Gleixner static void debug_object_is_on_stack(void *addr, int onstack)
5753ac7fe5aSThomas Gleixner {
5763ac7fe5aSThomas Gleixner 	int is_on_stack;
5773ac7fe5aSThomas Gleixner 	static int limit;
5783ac7fe5aSThomas Gleixner 
5793ac7fe5aSThomas Gleixner 	if (limit > 4)
5803ac7fe5aSThomas Gleixner 		return;
5813ac7fe5aSThomas Gleixner 
5828b05c7e6SFUJITA Tomonori 	is_on_stack = object_is_on_stack(addr);
5833ac7fe5aSThomas Gleixner 	if (is_on_stack == onstack)
5843ac7fe5aSThomas Gleixner 		return;
5853ac7fe5aSThomas Gleixner 
5863ac7fe5aSThomas Gleixner 	limit++;
5873ac7fe5aSThomas Gleixner 	if (is_on_stack)
588fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
589fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
5903ac7fe5aSThomas Gleixner 	else
591fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
592fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
593fc91a3c4SJoel Fernandes (Google) 
5943ac7fe5aSThomas Gleixner 	WARN_ON(1);
5953ac7fe5aSThomas Gleixner }
5963ac7fe5aSThomas Gleixner 
59763a75969SThomas Gleixner static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
59863a75969SThomas Gleixner 						const struct debug_obj_descr *descr,
59963a75969SThomas Gleixner 						bool onstack, bool alloc_ifstatic)
60063a75969SThomas Gleixner {
60163a75969SThomas Gleixner 	struct debug_obj *obj = lookup_object(addr, b);
60263a75969SThomas Gleixner 	enum debug_obj_state state = ODEBUG_STATE_NONE;
60363a75969SThomas Gleixner 
60463a75969SThomas Gleixner 	if (likely(obj))
60563a75969SThomas Gleixner 		return obj;
60663a75969SThomas Gleixner 
60763a75969SThomas Gleixner 	/*
60863a75969SThomas Gleixner 	 * debug_object_init() unconditionally allocates untracked
60963a75969SThomas Gleixner 	 * objects. It does not matter whether it is a static object or
61063a75969SThomas Gleixner 	 * not.
61163a75969SThomas Gleixner 	 *
61263a75969SThomas Gleixner 	 * debug_object_assert_init() and debug_object_activate() allow
61363a75969SThomas Gleixner 	 * allocation only if the descriptor callback confirms that the
61463a75969SThomas Gleixner 	 * object is static and considered initialized. For non-static
61563a75969SThomas Gleixner 	 * objects the allocation needs to be done from the fixup callback.
61663a75969SThomas Gleixner 	 */
61763a75969SThomas Gleixner 	if (unlikely(alloc_ifstatic)) {
61863a75969SThomas Gleixner 		if (!descr->is_static_object || !descr->is_static_object(addr))
61963a75969SThomas Gleixner 			return ERR_PTR(-ENOENT);
62063a75969SThomas Gleixner 		/* Statically allocated objects are considered initialized */
62163a75969SThomas Gleixner 		state = ODEBUG_STATE_INIT;
62263a75969SThomas Gleixner 	}
62363a75969SThomas Gleixner 
62463a75969SThomas Gleixner 	obj = alloc_object(addr, b, descr);
62563a75969SThomas Gleixner 	if (likely(obj)) {
62663a75969SThomas Gleixner 		obj->state = state;
62763a75969SThomas Gleixner 		debug_object_is_on_stack(addr, onstack);
62863a75969SThomas Gleixner 		return obj;
62963a75969SThomas Gleixner 	}
63063a75969SThomas Gleixner 
63163a75969SThomas Gleixner 	/* Out of memory. Do the cleanup outside of the locked region */
632661cc28bSThomas Gleixner 	debug_objects_enabled = false;
63363a75969SThomas Gleixner 	return NULL;
63463a75969SThomas Gleixner }
63563a75969SThomas Gleixner 
6360af462f1SThomas Gleixner static void debug_objects_fill_pool(void)
6370af462f1SThomas Gleixner {
638d8c6cd3aSZhen Lei 	if (unlikely(!obj_cache))
639d8c6cd3aSZhen Lei 		return;
640d8c6cd3aSZhen Lei 
641e18328ffSThomas Gleixner 	if (likely(!pool_global_should_refill()))
642d8c6cd3aSZhen Lei 		return;
643d8c6cd3aSZhen Lei 
644d8c6cd3aSZhen Lei 	/* Try reusing objects from obj_to_free_list */
645d8c6cd3aSZhen Lei 	fill_pool_from_freelist();
646d8c6cd3aSZhen Lei 
647e18328ffSThomas Gleixner 	if (likely(!pool_global_should_refill()))
648d8c6cd3aSZhen Lei 		return;
649d8c6cd3aSZhen Lei 
6500af462f1SThomas Gleixner 	/*
6510af462f1SThomas Gleixner 	 * On RT enabled kernels the pool refill must happen in preemptible
6520cce06baSPeter Zijlstra 	 * context -- for !RT kernels we rely on the fact that spinlock_t and
6530cce06baSPeter Zijlstra 	 * raw_spinlock_t are basically the same type and this lock-type
6540cce06baSPeter Zijlstra 	 * inversion works just fine.
6550af462f1SThomas Gleixner 	 */
6560cce06baSPeter Zijlstra 	if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible()) {
6570cce06baSPeter Zijlstra 		/*
6580cce06baSPeter Zijlstra 		 * Annotate away the spinlock_t inside raw_spinlock_t warning
6590cce06baSPeter Zijlstra 		 * by temporarily raising the wait-type to WAIT_SLEEP, matching
6600cce06baSPeter Zijlstra 		 * the preemptible() condition above.
6610cce06baSPeter Zijlstra 		 */
6620cce06baSPeter Zijlstra 		static DEFINE_WAIT_OVERRIDE_MAP(fill_pool_map, LD_WAIT_SLEEP);
6630cce06baSPeter Zijlstra 		lock_map_acquire_try(&fill_pool_map);
6640af462f1SThomas Gleixner 		fill_pool();
6650cce06baSPeter Zijlstra 		lock_map_release(&fill_pool_map);
6660cce06baSPeter Zijlstra 	}
6670af462f1SThomas Gleixner }
6680af462f1SThomas Gleixner 
6693ac7fe5aSThomas Gleixner static void
670aedcade6SStephen Boyd __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
6713ac7fe5aSThomas Gleixner {
6729bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
6733ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
6743ac7fe5aSThomas Gleixner 	unsigned long flags;
6753ac7fe5aSThomas Gleixner 
6760af462f1SThomas Gleixner 	debug_objects_fill_pool();
67750db04ddSVegard Nossum 
6783ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
6793ac7fe5aSThomas Gleixner 
680aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
6813ac7fe5aSThomas Gleixner 
68263a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
68363a75969SThomas Gleixner 	if (unlikely(!obj)) {
684aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
6853ac7fe5aSThomas Gleixner 		debug_objects_oom();
6863ac7fe5aSThomas Gleixner 		return;
6873ac7fe5aSThomas Gleixner 	}
6883ac7fe5aSThomas Gleixner 
6893ac7fe5aSThomas Gleixner 	switch (obj->state) {
6903ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
6913ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
6923ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
6933ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_INIT;
694aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
695d5f34153SWaiman Long 		return;
6963ac7fe5aSThomas Gleixner 	default:
6973ac7fe5aSThomas Gleixner 		break;
6983ac7fe5aSThomas Gleixner 	}
6993ac7fe5aSThomas Gleixner 
7009bb63626SAndrzej Hajda 	o = *obj;
701aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
7029bb63626SAndrzej Hajda 	debug_print_object(&o, "init");
7039bb63626SAndrzej Hajda 
7049bb63626SAndrzej Hajda 	if (o.state == ODEBUG_STATE_ACTIVE)
7059bb63626SAndrzej Hajda 		debug_object_fixup(descr->fixup_init, addr, o.state);
7063ac7fe5aSThomas Gleixner }
7073ac7fe5aSThomas Gleixner 
7083ac7fe5aSThomas Gleixner /**
7093ac7fe5aSThomas Gleixner  * debug_object_init - debug checks when an object is initialized
7103ac7fe5aSThomas Gleixner  * @addr:	address of the object
7113ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7123ac7fe5aSThomas Gleixner  */
713aedcade6SStephen Boyd void debug_object_init(void *addr, const struct debug_obj_descr *descr)
7143ac7fe5aSThomas Gleixner {
7153ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7163ac7fe5aSThomas Gleixner 		return;
7173ac7fe5aSThomas Gleixner 
7183ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 0);
7193ac7fe5aSThomas Gleixner }
720f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init);
7213ac7fe5aSThomas Gleixner 
7223ac7fe5aSThomas Gleixner /**
7233ac7fe5aSThomas Gleixner  * debug_object_init_on_stack - debug checks when an object on stack is
7243ac7fe5aSThomas Gleixner  *				initialized
7253ac7fe5aSThomas Gleixner  * @addr:	address of the object
7263ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7273ac7fe5aSThomas Gleixner  */
728aedcade6SStephen Boyd void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr)
7293ac7fe5aSThomas Gleixner {
7303ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7313ac7fe5aSThomas Gleixner 		return;
7323ac7fe5aSThomas Gleixner 
7333ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 1);
7343ac7fe5aSThomas Gleixner }
735f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
7363ac7fe5aSThomas Gleixner 
7373ac7fe5aSThomas Gleixner /**
7383ac7fe5aSThomas Gleixner  * debug_object_activate - debug checks when an object is activated
7393ac7fe5aSThomas Gleixner  * @addr:	address of the object
7403ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
741b778ae25SPaul E. McKenney  * Returns 0 for success, -EINVAL for check failed.
7423ac7fe5aSThomas Gleixner  */
743aedcade6SStephen Boyd int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
7443ac7fe5aSThomas Gleixner {
74563a75969SThomas Gleixner 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
7463ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
7473ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
7483ac7fe5aSThomas Gleixner 	unsigned long flags;
7493ac7fe5aSThomas Gleixner 
7503ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
751b778ae25SPaul E. McKenney 		return 0;
7523ac7fe5aSThomas Gleixner 
7530af462f1SThomas Gleixner 	debug_objects_fill_pool();
7540af462f1SThomas Gleixner 
7553ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
7563ac7fe5aSThomas Gleixner 
757aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
7583ac7fe5aSThomas Gleixner 
75963a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, false, true);
7609bb63626SAndrzej Hajda 	if (unlikely(!obj)) {
7619bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
7629bb63626SAndrzej Hajda 		debug_objects_oom();
7639bb63626SAndrzej Hajda 		return 0;
7649bb63626SAndrzej Hajda 	} else if (likely(!IS_ERR(obj))) {
7653ac7fe5aSThomas Gleixner 		switch (obj->state) {
7669bb63626SAndrzej Hajda 		case ODEBUG_STATE_ACTIVE:
7679bb63626SAndrzej Hajda 		case ODEBUG_STATE_DESTROYED:
7689bb63626SAndrzej Hajda 			o = *obj;
7699bb63626SAndrzej Hajda 			break;
7703ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
7713ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
7723ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_ACTIVE;
7739bb63626SAndrzej Hajda 			fallthrough;
7743ac7fe5aSThomas Gleixner 		default:
775aef9cb05SThomas Gleixner 			raw_spin_unlock_irqrestore(&db->lock, flags);
776b778ae25SPaul E. McKenney 			return 0;
7773ac7fe5aSThomas Gleixner 		}
7789bb63626SAndrzej Hajda 	}
77963a75969SThomas Gleixner 
7809bb63626SAndrzej Hajda 	raw_spin_unlock_irqrestore(&db->lock, flags);
78163a75969SThomas Gleixner 	debug_print_object(&o, "activate");
7829bb63626SAndrzej Hajda 
7839bb63626SAndrzej Hajda 	switch (o.state) {
7849bb63626SAndrzej Hajda 	case ODEBUG_STATE_ACTIVE:
7859bb63626SAndrzej Hajda 	case ODEBUG_STATE_NOTAVAILABLE:
7869bb63626SAndrzej Hajda 		if (debug_object_fixup(descr->fixup_activate, addr, o.state))
7879bb63626SAndrzej Hajda 			return 0;
7889bb63626SAndrzej Hajda 		fallthrough;
7899bb63626SAndrzej Hajda 	default:
7909bb63626SAndrzej Hajda 		return -EINVAL;
7919bb63626SAndrzej Hajda 	}
79263a75969SThomas Gleixner }
793f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_activate);
7943ac7fe5aSThomas Gleixner 
7953ac7fe5aSThomas Gleixner /**
7963ac7fe5aSThomas Gleixner  * debug_object_deactivate - debug checks when an object is deactivated
7973ac7fe5aSThomas Gleixner  * @addr:	address of the object
7983ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7993ac7fe5aSThomas Gleixner  */
800aedcade6SStephen Boyd void debug_object_deactivate(void *addr, const struct debug_obj_descr *descr)
8013ac7fe5aSThomas Gleixner {
8029bb63626SAndrzej Hajda 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
8033ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8043ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
8053ac7fe5aSThomas Gleixner 	unsigned long flags;
8063ac7fe5aSThomas Gleixner 
8073ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8083ac7fe5aSThomas Gleixner 		return;
8093ac7fe5aSThomas Gleixner 
8103ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8113ac7fe5aSThomas Gleixner 
812aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8133ac7fe5aSThomas Gleixner 
8143ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
8153ac7fe5aSThomas Gleixner 	if (obj) {
8163ac7fe5aSThomas Gleixner 		switch (obj->state) {
8179bb63626SAndrzej Hajda 		case ODEBUG_STATE_DESTROYED:
8189bb63626SAndrzej Hajda 			break;
8193ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
8203ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
8213ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_ACTIVE:
8229bb63626SAndrzej Hajda 			if (obj->astate)
8239bb63626SAndrzej Hajda 				break;
8243ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_INACTIVE;
8259bb63626SAndrzej Hajda 			fallthrough;
8263ac7fe5aSThomas Gleixner 		default:
8279bb63626SAndrzej Hajda 			raw_spin_unlock_irqrestore(&db->lock, flags);
8289bb63626SAndrzej Hajda 			return;
8293ac7fe5aSThomas Gleixner 		}
8309bb63626SAndrzej Hajda 		o = *obj;
831d5f34153SWaiman Long 	}
832d5f34153SWaiman Long 
833d5f34153SWaiman Long 	raw_spin_unlock_irqrestore(&db->lock, flags);
8343ac7fe5aSThomas Gleixner 	debug_print_object(&o, "deactivate");
8353ac7fe5aSThomas Gleixner }
836f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_deactivate);
8373ac7fe5aSThomas Gleixner 
8383ac7fe5aSThomas Gleixner /**
8393ac7fe5aSThomas Gleixner  * debug_object_destroy - debug checks when an object is destroyed
8403ac7fe5aSThomas Gleixner  * @addr:	address of the object
8413ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
8423ac7fe5aSThomas Gleixner  */
843aedcade6SStephen Boyd void debug_object_destroy(void *addr, const struct debug_obj_descr *descr)
8443ac7fe5aSThomas Gleixner {
8459bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
8463ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8473ac7fe5aSThomas Gleixner 	unsigned long flags;
8483ac7fe5aSThomas Gleixner 
8493ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8503ac7fe5aSThomas Gleixner 		return;
8513ac7fe5aSThomas Gleixner 
8523ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8533ac7fe5aSThomas Gleixner 
854aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8553ac7fe5aSThomas Gleixner 
8563ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
8579bb63626SAndrzej Hajda 	if (!obj) {
8589bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
8599bb63626SAndrzej Hajda 		return;
8609bb63626SAndrzej Hajda 	}
8613ac7fe5aSThomas Gleixner 
8623ac7fe5aSThomas Gleixner 	switch (obj->state) {
8639bb63626SAndrzej Hajda 	case ODEBUG_STATE_ACTIVE:
8649bb63626SAndrzej Hajda 	case ODEBUG_STATE_DESTROYED:
8659bb63626SAndrzej Hajda 		break;
8663ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
8673ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
8683ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
8693ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_DESTROYED;
8709bb63626SAndrzej Hajda 		fallthrough;
8713ac7fe5aSThomas Gleixner 	default:
872aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
8739bb63626SAndrzej Hajda 		return;
8749bb63626SAndrzej Hajda 	}
8759bb63626SAndrzej Hajda 
8769bb63626SAndrzej Hajda 	o = *obj;
8779bb63626SAndrzej Hajda 	raw_spin_unlock_irqrestore(&db->lock, flags);
8789bb63626SAndrzej Hajda 	debug_print_object(&o, "destroy");
8799bb63626SAndrzej Hajda 
8809bb63626SAndrzej Hajda 	if (o.state == ODEBUG_STATE_ACTIVE)
8819bb63626SAndrzej Hajda 		debug_object_fixup(descr->fixup_destroy, addr, o.state);
8823ac7fe5aSThomas Gleixner }
883f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_destroy);
8843ac7fe5aSThomas Gleixner 
8853ac7fe5aSThomas Gleixner /**
8863ac7fe5aSThomas Gleixner  * debug_object_free - debug checks when an object is freed
8873ac7fe5aSThomas Gleixner  * @addr:	address of the object
8883ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
8893ac7fe5aSThomas Gleixner  */
890aedcade6SStephen Boyd void debug_object_free(void *addr, const struct debug_obj_descr *descr)
8913ac7fe5aSThomas Gleixner {
8929bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
8933ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8943ac7fe5aSThomas Gleixner 	unsigned long flags;
8953ac7fe5aSThomas Gleixner 
8963ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8973ac7fe5aSThomas Gleixner 		return;
8983ac7fe5aSThomas Gleixner 
8993ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
9003ac7fe5aSThomas Gleixner 
901aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
9023ac7fe5aSThomas Gleixner 
9033ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
9049bb63626SAndrzej Hajda 	if (!obj) {
9059bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
9069bb63626SAndrzej Hajda 		return;
9079bb63626SAndrzej Hajda 	}
9083ac7fe5aSThomas Gleixner 
9093ac7fe5aSThomas Gleixner 	switch (obj->state) {
9103ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
9119bb63626SAndrzej Hajda 		break;
9123ac7fe5aSThomas Gleixner 	default:
9133ac7fe5aSThomas Gleixner 		hlist_del(&obj->node);
914aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
9153ac7fe5aSThomas Gleixner 		free_object(obj);
916673d62ccSVegard Nossum 		return;
9173ac7fe5aSThomas Gleixner 	}
9189bb63626SAndrzej Hajda 
9199bb63626SAndrzej Hajda 	o = *obj;
920aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
9219bb63626SAndrzej Hajda 	debug_print_object(&o, "free");
9229bb63626SAndrzej Hajda 
9239bb63626SAndrzej Hajda 	debug_object_fixup(descr->fixup_free, addr, o.state);
9243ac7fe5aSThomas Gleixner }
925f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_free);
9263ac7fe5aSThomas Gleixner 
927a5d8e467SMathieu Desnoyers /**
928b84d435cSChristine Chan  * debug_object_assert_init - debug checks when object should be init-ed
929b84d435cSChristine Chan  * @addr:	address of the object
930b84d435cSChristine Chan  * @descr:	pointer to an object specific debug description structure
931b84d435cSChristine Chan  */
932aedcade6SStephen Boyd void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
933b84d435cSChristine Chan {
93463a75969SThomas Gleixner 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
935b84d435cSChristine Chan 	struct debug_bucket *db;
936b84d435cSChristine Chan 	struct debug_obj *obj;
937b84d435cSChristine Chan 	unsigned long flags;
938b84d435cSChristine Chan 
939b84d435cSChristine Chan 	if (!debug_objects_enabled)
940b84d435cSChristine Chan 		return;
941b84d435cSChristine Chan 
9420af462f1SThomas Gleixner 	debug_objects_fill_pool();
9430af462f1SThomas Gleixner 
944b84d435cSChristine Chan 	db = get_bucket((unsigned long) addr);
945b84d435cSChristine Chan 
946b84d435cSChristine Chan 	raw_spin_lock_irqsave(&db->lock, flags);
94763a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, false, true);
948b84d435cSChristine Chan 	raw_spin_unlock_irqrestore(&db->lock, flags);
94963a75969SThomas Gleixner 	if (likely(!IS_ERR_OR_NULL(obj)))
95063a75969SThomas Gleixner 		return;
95163a75969SThomas Gleixner 
95263a75969SThomas Gleixner 	/* If NULL the allocation has hit OOM */
95363a75969SThomas Gleixner 	if (!obj) {
95463a75969SThomas Gleixner 		debug_objects_oom();
955b84d435cSChristine Chan 		return;
956b84d435cSChristine Chan 	}
957b84d435cSChristine Chan 
95863a75969SThomas Gleixner 	/* Object is neither tracked nor static. It's not initialized. */
95963a75969SThomas Gleixner 	debug_print_object(&o, "assert_init");
96063a75969SThomas Gleixner 	debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
961b84d435cSChristine Chan }
962f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_assert_init);
963b84d435cSChristine Chan 
964b84d435cSChristine Chan /**
965a5d8e467SMathieu Desnoyers  * debug_object_active_state - debug checks object usage state machine
966a5d8e467SMathieu Desnoyers  * @addr:	address of the object
967a5d8e467SMathieu Desnoyers  * @descr:	pointer to an object specific debug description structure
968a5d8e467SMathieu Desnoyers  * @expect:	expected state
969a5d8e467SMathieu Desnoyers  * @next:	state to move to if expected state is found
970a5d8e467SMathieu Desnoyers  */
971a5d8e467SMathieu Desnoyers void
972aedcade6SStephen Boyd debug_object_active_state(void *addr, const struct debug_obj_descr *descr,
973a5d8e467SMathieu Desnoyers 			  unsigned int expect, unsigned int next)
974a5d8e467SMathieu Desnoyers {
9759bb63626SAndrzej Hajda 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
976a5d8e467SMathieu Desnoyers 	struct debug_bucket *db;
977a5d8e467SMathieu Desnoyers 	struct debug_obj *obj;
978a5d8e467SMathieu Desnoyers 	unsigned long flags;
979a5d8e467SMathieu Desnoyers 
980a5d8e467SMathieu Desnoyers 	if (!debug_objects_enabled)
981a5d8e467SMathieu Desnoyers 		return;
982a5d8e467SMathieu Desnoyers 
983a5d8e467SMathieu Desnoyers 	db = get_bucket((unsigned long) addr);
984a5d8e467SMathieu Desnoyers 
985a5d8e467SMathieu Desnoyers 	raw_spin_lock_irqsave(&db->lock, flags);
986a5d8e467SMathieu Desnoyers 
987a5d8e467SMathieu Desnoyers 	obj = lookup_object(addr, db);
988a5d8e467SMathieu Desnoyers 	if (obj) {
989a5d8e467SMathieu Desnoyers 		switch (obj->state) {
990a5d8e467SMathieu Desnoyers 		case ODEBUG_STATE_ACTIVE:
9919bb63626SAndrzej Hajda 			if (obj->astate != expect)
992a5d8e467SMathieu Desnoyers 				break;
9939bb63626SAndrzej Hajda 			obj->astate = next;
9949bb63626SAndrzej Hajda 			raw_spin_unlock_irqrestore(&db->lock, flags);
9959bb63626SAndrzej Hajda 			return;
996a5d8e467SMathieu Desnoyers 		default:
997a5d8e467SMathieu Desnoyers 			break;
998a5d8e467SMathieu Desnoyers 		}
9999bb63626SAndrzej Hajda 		o = *obj;
1000d5f34153SWaiman Long 	}
1001d5f34153SWaiman Long 
1002d5f34153SWaiman Long 	raw_spin_unlock_irqrestore(&db->lock, flags);
1003a5d8e467SMathieu Desnoyers 	debug_print_object(&o, "active_state");
1004a5d8e467SMathieu Desnoyers }
1005f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_active_state);
1006a5d8e467SMathieu Desnoyers 
10073ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
10083ac7fe5aSThomas Gleixner static void __debug_check_no_obj_freed(const void *address, unsigned long size)
10093ac7fe5aSThomas Gleixner {
10103ac7fe5aSThomas Gleixner 	unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
10119bb63626SAndrzej Hajda 	int cnt, objs_checked = 0;
10129bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
10133ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
10141ea9b98bSYang Shi 	struct hlist_node *tmp;
10153ac7fe5aSThomas Gleixner 
10163ac7fe5aSThomas Gleixner 	saddr = (unsigned long) address;
10173ac7fe5aSThomas Gleixner 	eaddr = saddr + size;
10183ac7fe5aSThomas Gleixner 	paddr = saddr & ODEBUG_CHUNK_MASK;
10193ac7fe5aSThomas Gleixner 	chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
10203ac7fe5aSThomas Gleixner 	chunks >>= ODEBUG_CHUNK_SHIFT;
10213ac7fe5aSThomas Gleixner 
10223ac7fe5aSThomas Gleixner 	for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
10233ac7fe5aSThomas Gleixner 		db = get_bucket(paddr);
10243ac7fe5aSThomas Gleixner 
10253ac7fe5aSThomas Gleixner repeat:
10263ac7fe5aSThomas Gleixner 		cnt = 0;
1027aef9cb05SThomas Gleixner 		raw_spin_lock_irqsave(&db->lock, flags);
1028b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
10293ac7fe5aSThomas Gleixner 			cnt++;
10303ac7fe5aSThomas Gleixner 			oaddr = (unsigned long) obj->object;
10313ac7fe5aSThomas Gleixner 			if (oaddr < saddr || oaddr >= eaddr)
10323ac7fe5aSThomas Gleixner 				continue;
10333ac7fe5aSThomas Gleixner 
10343ac7fe5aSThomas Gleixner 			switch (obj->state) {
10353ac7fe5aSThomas Gleixner 			case ODEBUG_STATE_ACTIVE:
10369bb63626SAndrzej Hajda 				o = *obj;
1037aef9cb05SThomas Gleixner 				raw_spin_unlock_irqrestore(&db->lock, flags);
10389bb63626SAndrzej Hajda 				debug_print_object(&o, "free");
10399bb63626SAndrzej Hajda 				debug_object_fixup(o.descr->fixup_free, (void *)oaddr, o.state);
10403ac7fe5aSThomas Gleixner 				goto repeat;
10413ac7fe5aSThomas Gleixner 			default:
10423ac7fe5aSThomas Gleixner 				hlist_del(&obj->node);
1043a7344a68SWaiman Long 				__free_object(obj);
10443ac7fe5aSThomas Gleixner 				break;
10453ac7fe5aSThomas Gleixner 			}
10463ac7fe5aSThomas Gleixner 		}
1047aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
1048673d62ccSVegard Nossum 
10493ac7fe5aSThomas Gleixner 		if (cnt > debug_objects_maxchain)
10503ac7fe5aSThomas Gleixner 			debug_objects_maxchain = cnt;
1051bd9dcd04SYang Shi 
1052bd9dcd04SYang Shi 		objs_checked += cnt;
10533ac7fe5aSThomas Gleixner 	}
1054bd9dcd04SYang Shi 
1055bd9dcd04SYang Shi 	if (objs_checked > debug_objects_maxchecked)
1056bd9dcd04SYang Shi 		debug_objects_maxchecked = objs_checked;
10571ea9b98bSYang Shi 
10581ea9b98bSYang Shi 	/* Schedule work to actually kmem_cache_free() objects */
1059e18328ffSThomas Gleixner 	if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
1060a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
1061a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
1062a7344a68SWaiman Long 	}
10633ac7fe5aSThomas Gleixner }
10643ac7fe5aSThomas Gleixner 
10653ac7fe5aSThomas Gleixner void debug_check_no_obj_freed(const void *address, unsigned long size)
10663ac7fe5aSThomas Gleixner {
10673ac7fe5aSThomas Gleixner 	if (debug_objects_enabled)
10683ac7fe5aSThomas Gleixner 		__debug_check_no_obj_freed(address, size);
10693ac7fe5aSThomas Gleixner }
10703ac7fe5aSThomas Gleixner #endif
10713ac7fe5aSThomas Gleixner 
10723ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_FS
10733ac7fe5aSThomas Gleixner 
10743ac7fe5aSThomas Gleixner static int debug_stats_show(struct seq_file *m, void *v)
10753ac7fe5aSThomas Gleixner {
1076d86998b1SWaiman Long 	int cpu, obj_percpu_free = 0;
1077d86998b1SWaiman Long 
1078d86998b1SWaiman Long 	for_each_possible_cpu(cpu)
1079d86998b1SWaiman Long 		obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu);
1080d86998b1SWaiman Long 
10813ac7fe5aSThomas Gleixner 	seq_printf(m, "max_chain     :%d\n", debug_objects_maxchain);
1082bd9dcd04SYang Shi 	seq_printf(m, "max_checked   :%d\n", debug_objects_maxchecked);
10833ac7fe5aSThomas Gleixner 	seq_printf(m, "warnings      :%d\n", debug_objects_warnings);
10843ac7fe5aSThomas Gleixner 	seq_printf(m, "fixups        :%d\n", debug_objects_fixups);
1085e18328ffSThomas Gleixner 	seq_printf(m, "pool_free     :%d\n", pool_count(&pool_global) + obj_percpu_free);
1086d86998b1SWaiman Long 	seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
10873ac7fe5aSThomas Gleixner 	seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
1088d86998b1SWaiman Long 	seq_printf(m, "pool_used     :%d\n", obj_pool_used - obj_percpu_free);
10893ac7fe5aSThomas Gleixner 	seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
1090e18328ffSThomas Gleixner 	seq_printf(m, "on_free_list  :%d\n", pool_count(&pool_to_free));
10910cad93c3SWaiman Long 	seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
10920cad93c3SWaiman Long 	seq_printf(m, "objs_freed    :%d\n", debug_objects_freed);
10933ac7fe5aSThomas Gleixner 	return 0;
10943ac7fe5aSThomas Gleixner }
10950f85c480SQinglang Miao DEFINE_SHOW_ATTRIBUTE(debug_stats);
10963ac7fe5aSThomas Gleixner 
10973ac7fe5aSThomas Gleixner static int __init debug_objects_init_debugfs(void)
10983ac7fe5aSThomas Gleixner {
1099fecb0d95SGreg Kroah-Hartman 	struct dentry *dbgdir;
11003ac7fe5aSThomas Gleixner 
11013ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
11023ac7fe5aSThomas Gleixner 		return 0;
11033ac7fe5aSThomas Gleixner 
11043ac7fe5aSThomas Gleixner 	dbgdir = debugfs_create_dir("debug_objects", NULL);
11053ac7fe5aSThomas Gleixner 
1106fecb0d95SGreg Kroah-Hartman 	debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
11073ac7fe5aSThomas Gleixner 
11083ac7fe5aSThomas Gleixner 	return 0;
11093ac7fe5aSThomas Gleixner }
11103ac7fe5aSThomas Gleixner __initcall(debug_objects_init_debugfs);
11113ac7fe5aSThomas Gleixner 
11123ac7fe5aSThomas Gleixner #else
11133ac7fe5aSThomas Gleixner static inline void debug_objects_init_debugfs(void) { }
11143ac7fe5aSThomas Gleixner #endif
11153ac7fe5aSThomas Gleixner 
11163ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
11173ac7fe5aSThomas Gleixner 
11183ac7fe5aSThomas Gleixner /* Random data structure for the self test */
11193ac7fe5aSThomas Gleixner struct self_test {
11203ac7fe5aSThomas Gleixner 	unsigned long	dummy1[6];
11213ac7fe5aSThomas Gleixner 	int		static_init;
11223ac7fe5aSThomas Gleixner 	unsigned long	dummy2[3];
11233ac7fe5aSThomas Gleixner };
11243ac7fe5aSThomas Gleixner 
1125aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test;
11263ac7fe5aSThomas Gleixner 
1127b9fdac7fSDu, Changbin static bool __init is_static_object(void *addr)
1128b9fdac7fSDu, Changbin {
1129b9fdac7fSDu, Changbin 	struct self_test *obj = addr;
1130b9fdac7fSDu, Changbin 
1131b9fdac7fSDu, Changbin 	return obj->static_init;
1132b9fdac7fSDu, Changbin }
1133b9fdac7fSDu, Changbin 
11343ac7fe5aSThomas Gleixner /*
11353ac7fe5aSThomas Gleixner  * fixup_init is called when:
11363ac7fe5aSThomas Gleixner  * - an active object is initialized
11373ac7fe5aSThomas Gleixner  */
1138b1e4d9d8SDu, Changbin static bool __init fixup_init(void *addr, enum debug_obj_state state)
11393ac7fe5aSThomas Gleixner {
11403ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11413ac7fe5aSThomas Gleixner 
11423ac7fe5aSThomas Gleixner 	switch (state) {
11433ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11443ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11453ac7fe5aSThomas Gleixner 		debug_object_init(obj, &descr_type_test);
1146b1e4d9d8SDu, Changbin 		return true;
11473ac7fe5aSThomas Gleixner 	default:
1148b1e4d9d8SDu, Changbin 		return false;
11493ac7fe5aSThomas Gleixner 	}
11503ac7fe5aSThomas Gleixner }
11513ac7fe5aSThomas Gleixner 
11523ac7fe5aSThomas Gleixner /*
11533ac7fe5aSThomas Gleixner  * fixup_activate is called when:
11543ac7fe5aSThomas Gleixner  * - an active object is activated
1155b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
11563ac7fe5aSThomas Gleixner  */
1157b1e4d9d8SDu, Changbin static bool __init fixup_activate(void *addr, enum debug_obj_state state)
11583ac7fe5aSThomas Gleixner {
11593ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11603ac7fe5aSThomas Gleixner 
11613ac7fe5aSThomas Gleixner 	switch (state) {
11623ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
1163b1e4d9d8SDu, Changbin 		return true;
11643ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11653ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11663ac7fe5aSThomas Gleixner 		debug_object_activate(obj, &descr_type_test);
1167b1e4d9d8SDu, Changbin 		return true;
11683ac7fe5aSThomas Gleixner 
11693ac7fe5aSThomas Gleixner 	default:
1170b1e4d9d8SDu, Changbin 		return false;
11713ac7fe5aSThomas Gleixner 	}
11723ac7fe5aSThomas Gleixner }
11733ac7fe5aSThomas Gleixner 
11743ac7fe5aSThomas Gleixner /*
11753ac7fe5aSThomas Gleixner  * fixup_destroy is called when:
11763ac7fe5aSThomas Gleixner  * - an active object is destroyed
11773ac7fe5aSThomas Gleixner  */
1178b1e4d9d8SDu, Changbin static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
11793ac7fe5aSThomas Gleixner {
11803ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11813ac7fe5aSThomas Gleixner 
11823ac7fe5aSThomas Gleixner 	switch (state) {
11833ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11843ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11853ac7fe5aSThomas Gleixner 		debug_object_destroy(obj, &descr_type_test);
1186b1e4d9d8SDu, Changbin 		return true;
11873ac7fe5aSThomas Gleixner 	default:
1188b1e4d9d8SDu, Changbin 		return false;
11893ac7fe5aSThomas Gleixner 	}
11903ac7fe5aSThomas Gleixner }
11913ac7fe5aSThomas Gleixner 
11923ac7fe5aSThomas Gleixner /*
11933ac7fe5aSThomas Gleixner  * fixup_free is called when:
11943ac7fe5aSThomas Gleixner  * - an active object is freed
11953ac7fe5aSThomas Gleixner  */
1196b1e4d9d8SDu, Changbin static bool __init fixup_free(void *addr, enum debug_obj_state state)
11973ac7fe5aSThomas Gleixner {
11983ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11993ac7fe5aSThomas Gleixner 
12003ac7fe5aSThomas Gleixner 	switch (state) {
12013ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
12023ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
12033ac7fe5aSThomas Gleixner 		debug_object_free(obj, &descr_type_test);
1204b1e4d9d8SDu, Changbin 		return true;
12053ac7fe5aSThomas Gleixner 	default:
1206b1e4d9d8SDu, Changbin 		return false;
12073ac7fe5aSThomas Gleixner 	}
12083ac7fe5aSThomas Gleixner }
12093ac7fe5aSThomas Gleixner 
12101fb2f77cSHenrik Kretzschmar static int __init
12113ac7fe5aSThomas Gleixner check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
12123ac7fe5aSThomas Gleixner {
12133ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
12143ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
12153ac7fe5aSThomas Gleixner 	unsigned long flags;
12163ac7fe5aSThomas Gleixner 	int res = -EINVAL;
12173ac7fe5aSThomas Gleixner 
12183ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
12193ac7fe5aSThomas Gleixner 
1220aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
12213ac7fe5aSThomas Gleixner 
12223ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
12233ac7fe5aSThomas Gleixner 	if (!obj && state != ODEBUG_STATE_NONE) {
12245cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
12253ac7fe5aSThomas Gleixner 		goto out;
12263ac7fe5aSThomas Gleixner 	}
12273ac7fe5aSThomas Gleixner 	if (obj && obj->state != state) {
12285cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
12293ac7fe5aSThomas Gleixner 		       obj->state, state);
12303ac7fe5aSThomas Gleixner 		goto out;
12313ac7fe5aSThomas Gleixner 	}
12323ac7fe5aSThomas Gleixner 	if (fixups != debug_objects_fixups) {
12335cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
12343ac7fe5aSThomas Gleixner 		       fixups, debug_objects_fixups);
12353ac7fe5aSThomas Gleixner 		goto out;
12363ac7fe5aSThomas Gleixner 	}
12373ac7fe5aSThomas Gleixner 	if (warnings != debug_objects_warnings) {
12385cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
12393ac7fe5aSThomas Gleixner 		       warnings, debug_objects_warnings);
12403ac7fe5aSThomas Gleixner 		goto out;
12413ac7fe5aSThomas Gleixner 	}
12423ac7fe5aSThomas Gleixner 	res = 0;
12433ac7fe5aSThomas Gleixner out:
1244aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
12453ac7fe5aSThomas Gleixner 	if (res)
1246661cc28bSThomas Gleixner 		debug_objects_enabled = false;
12473ac7fe5aSThomas Gleixner 	return res;
12483ac7fe5aSThomas Gleixner }
12493ac7fe5aSThomas Gleixner 
1250aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test = {
12513ac7fe5aSThomas Gleixner 	.name			= "selftest",
1252b9fdac7fSDu, Changbin 	.is_static_object	= is_static_object,
12533ac7fe5aSThomas Gleixner 	.fixup_init		= fixup_init,
12543ac7fe5aSThomas Gleixner 	.fixup_activate		= fixup_activate,
12553ac7fe5aSThomas Gleixner 	.fixup_destroy		= fixup_destroy,
12563ac7fe5aSThomas Gleixner 	.fixup_free		= fixup_free,
12573ac7fe5aSThomas Gleixner };
12583ac7fe5aSThomas Gleixner 
12593ac7fe5aSThomas Gleixner static __initdata struct self_test obj = { .static_init = 0 };
12603ac7fe5aSThomas Gleixner 
126155fb412eSThomas Gleixner static bool __init debug_objects_selftest(void)
12623ac7fe5aSThomas Gleixner {
12633ac7fe5aSThomas Gleixner 	int fixups, oldfixups, warnings, oldwarnings;
12643ac7fe5aSThomas Gleixner 	unsigned long flags;
12653ac7fe5aSThomas Gleixner 
12663ac7fe5aSThomas Gleixner 	local_irq_save(flags);
12673ac7fe5aSThomas Gleixner 
12683ac7fe5aSThomas Gleixner 	fixups = oldfixups = debug_objects_fixups;
12693ac7fe5aSThomas Gleixner 	warnings = oldwarnings = debug_objects_warnings;
12703ac7fe5aSThomas Gleixner 	descr_test = &descr_type_test;
12713ac7fe5aSThomas Gleixner 
12723ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12733ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
12743ac7fe5aSThomas Gleixner 		goto out;
12753ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12763ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
12773ac7fe5aSThomas Gleixner 		goto out;
12783ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12793ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
12803ac7fe5aSThomas Gleixner 		goto out;
12813ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
12823ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
12833ac7fe5aSThomas Gleixner 		goto out;
12843ac7fe5aSThomas Gleixner 	debug_object_destroy(&obj, &descr_type_test);
12853ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
12863ac7fe5aSThomas Gleixner 		goto out;
12873ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12883ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12893ac7fe5aSThomas Gleixner 		goto out;
12903ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12913ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12923ac7fe5aSThomas Gleixner 		goto out;
12933ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
12943ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12953ac7fe5aSThomas Gleixner 		goto out;
12963ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
12973ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
12983ac7fe5aSThomas Gleixner 		goto out;
12993ac7fe5aSThomas Gleixner 
13003ac7fe5aSThomas Gleixner 	obj.static_init = 1;
13013ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13029f78ff00SStephen Boyd 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
13033ac7fe5aSThomas Gleixner 		goto out;
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_free(&obj, &descr_type_test);
13083ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
13093ac7fe5aSThomas Gleixner 		goto out;
13103ac7fe5aSThomas Gleixner 
13113ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
13123ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
13133ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
13143ac7fe5aSThomas Gleixner 		goto out;
13153ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13163ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
13173ac7fe5aSThomas Gleixner 		goto out;
13183ac7fe5aSThomas Gleixner 	__debug_check_no_obj_freed(&obj, sizeof(obj));
13193ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
13203ac7fe5aSThomas Gleixner 		goto out;
13213ac7fe5aSThomas Gleixner #endif
1322719e4843SFabian Frederick 	pr_info("selftest passed\n");
13233ac7fe5aSThomas Gleixner 
13243ac7fe5aSThomas Gleixner out:
13253ac7fe5aSThomas Gleixner 	debug_objects_fixups = oldfixups;
13263ac7fe5aSThomas Gleixner 	debug_objects_warnings = oldwarnings;
13273ac7fe5aSThomas Gleixner 	descr_test = NULL;
13283ac7fe5aSThomas Gleixner 
13293ac7fe5aSThomas Gleixner 	local_irq_restore(flags);
1330661cc28bSThomas Gleixner 	return debug_objects_enabled;
13313ac7fe5aSThomas Gleixner }
13323ac7fe5aSThomas Gleixner #else
133355fb412eSThomas Gleixner static inline bool debug_objects_selftest(void) { return true; }
13343ac7fe5aSThomas Gleixner #endif
13353ac7fe5aSThomas Gleixner 
13363ac7fe5aSThomas Gleixner /*
13373ac7fe5aSThomas Gleixner  * Called during early boot to initialize the hash buckets and link
13383ac7fe5aSThomas Gleixner  * the static object pool objects into the poll list. After this call
13393ac7fe5aSThomas Gleixner  * the object tracker is fully operational.
13403ac7fe5aSThomas Gleixner  */
13413ac7fe5aSThomas Gleixner void __init debug_objects_early_init(void)
13423ac7fe5aSThomas Gleixner {
13433ac7fe5aSThomas Gleixner 	int i;
13443ac7fe5aSThomas Gleixner 
13453ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1346aef9cb05SThomas Gleixner 		raw_spin_lock_init(&obj_hash[i].lock);
13473ac7fe5aSThomas Gleixner 
1348*cb58d190SThomas Gleixner 	/* Keep early boot simple and add everything to the boot list */
13493ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1350*cb58d190SThomas Gleixner 		hlist_add_head(&obj_static_pool[i].node, &pool_boot);
13513ac7fe5aSThomas Gleixner }
13523ac7fe5aSThomas Gleixner 
13533ac7fe5aSThomas Gleixner /*
135455fb412eSThomas Gleixner  * Convert the statically allocated objects to dynamic ones.
135555fb412eSThomas Gleixner  * debug_objects_mem_init() is called early so only one CPU is up and
135655fb412eSThomas Gleixner  * interrupts are disabled, which means it is safe to replace the active
135755fb412eSThomas Gleixner  * object references.
13581be1cb7bSThomas Gleixner  */
135955fb412eSThomas Gleixner static bool __init debug_objects_replace_static_objects(struct kmem_cache *cache)
13601be1cb7bSThomas Gleixner {
13611be1cb7bSThomas Gleixner 	struct debug_bucket *db = obj_hash;
13621be1cb7bSThomas Gleixner 	struct debug_obj *obj, *new;
136355fb412eSThomas Gleixner 	struct hlist_node *tmp;
13641be1cb7bSThomas Gleixner 	HLIST_HEAD(objects);
1365241463f4SThomas Gleixner 	int i;
13661be1cb7bSThomas Gleixner 
13671be1cb7bSThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
136855fb412eSThomas Gleixner 		obj = kmem_cache_zalloc(cache, GFP_KERNEL);
13691be1cb7bSThomas Gleixner 		if (!obj)
13701be1cb7bSThomas Gleixner 			goto free;
13711be1cb7bSThomas Gleixner 		hlist_add_head(&obj->node, &objects);
13721be1cb7bSThomas Gleixner 	}
13731be1cb7bSThomas Gleixner 
1374e18328ffSThomas Gleixner 	debug_objects_allocated = ODEBUG_POOL_SIZE;
1375e18328ffSThomas Gleixner 	pool_global.cnt = ODEBUG_POOL_SIZE;
1376eabb7f1aSwuchi 
13771be1cb7bSThomas Gleixner 	/*
1378*cb58d190SThomas Gleixner 	 * Move the allocated objects to the global pool and disconnect the
1379*cb58d190SThomas Gleixner 	 * boot pool.
1380a0ae9504SZhen Lei 	 */
1381e18328ffSThomas Gleixner 	hlist_move_list(&objects, &pool_global.objects);
1382*cb58d190SThomas Gleixner 	pool_boot.first = NULL;
13831be1cb7bSThomas Gleixner 
13841be1cb7bSThomas Gleixner 	/* Replace the active object references */
13851be1cb7bSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
13861be1cb7bSThomas Gleixner 		hlist_move_list(&db->list, &objects);
13871be1cb7bSThomas Gleixner 
1388b67bfe0dSSasha Levin 		hlist_for_each_entry(obj, &objects, node) {
1389e18328ffSThomas Gleixner 			new = hlist_entry(pool_global.objects.first, typeof(*obj), node);
13901be1cb7bSThomas Gleixner 			hlist_del(&new->node);
1391e18328ffSThomas Gleixner 			pool_global.cnt--;
13921be1cb7bSThomas Gleixner 			/* copy object data */
13931be1cb7bSThomas Gleixner 			*new = *obj;
13941be1cb7bSThomas Gleixner 			hlist_add_head(&new->node, &db->list);
13951be1cb7bSThomas Gleixner 		}
13961be1cb7bSThomas Gleixner 	}
139755fb412eSThomas Gleixner 	return true;
13981be1cb7bSThomas Gleixner free:
139949a5cb82SThomas Gleixner 	/* Can't use free_object_list() as the cache is not populated yet */
1400b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(obj, tmp, &objects, node) {
14011be1cb7bSThomas Gleixner 		hlist_del(&obj->node);
140255fb412eSThomas Gleixner 		kmem_cache_free(cache, obj);
14031be1cb7bSThomas Gleixner 	}
140455fb412eSThomas Gleixner 	return false;
14051be1cb7bSThomas Gleixner }
14061be1cb7bSThomas Gleixner 
14071be1cb7bSThomas Gleixner /*
14083ac7fe5aSThomas Gleixner  * Called after the kmem_caches are functional to setup a dedicated
14093ac7fe5aSThomas Gleixner  * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
14103ac7fe5aSThomas Gleixner  * prevents that the debug code is called on kmem_cache_free() for the
14113ac7fe5aSThomas Gleixner  * debug tracker objects to avoid recursive calls.
14123ac7fe5aSThomas Gleixner  */
14133ac7fe5aSThomas Gleixner void __init debug_objects_mem_init(void)
14143ac7fe5aSThomas Gleixner {
141555fb412eSThomas Gleixner 	struct kmem_cache *cache;
14163f397bf9SThomas Gleixner 	int extras;
1417d86998b1SWaiman Long 
14183ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
14193ac7fe5aSThomas Gleixner 		return;
14203ac7fe5aSThomas Gleixner 
142155fb412eSThomas Gleixner 	if (!debug_objects_selftest())
1422eabb7f1aSwuchi 		return;
142355fb412eSThomas Gleixner 
142455fb412eSThomas Gleixner 	cache = kmem_cache_create("debug_objects_cache", sizeof (struct debug_obj), 0,
142555fb412eSThomas Gleixner 				  SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE, NULL);
142655fb412eSThomas Gleixner 
142755fb412eSThomas Gleixner 	if (!cache || !debug_objects_replace_static_objects(cache)) {
1428661cc28bSThomas Gleixner 		debug_objects_enabled = false;
142955fb412eSThomas Gleixner 		pr_warn("Out of memory.\n");
143055fb412eSThomas Gleixner 		return;
143155fb412eSThomas Gleixner 	}
143255fb412eSThomas Gleixner 
143355fb412eSThomas Gleixner 	/*
143455fb412eSThomas Gleixner 	 * Adjust the thresholds for allocating and freeing objects
143555fb412eSThomas Gleixner 	 * according to the number of possible CPUs available in the
143655fb412eSThomas Gleixner 	 * system.
143755fb412eSThomas Gleixner 	 */
143855fb412eSThomas Gleixner 	extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
143955fb412eSThomas Gleixner 	debug_objects_pool_size += extras;
144055fb412eSThomas Gleixner 	debug_objects_pool_min_level += extras;
144155fb412eSThomas Gleixner 
144255fb412eSThomas Gleixner 	/* Everything worked. Expose the cache */
144355fb412eSThomas Gleixner 	obj_cache = cache;
1444634d61f4SWaiman Long 
144588451f2cSZqiang #ifdef CONFIG_HOTPLUG_CPU
144688451f2cSZqiang 	cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL,
144788451f2cSZqiang 				  object_cpu_offline);
144888451f2cSZqiang #endif
144955fb412eSThomas Gleixner 	return;
14503ac7fe5aSThomas Gleixner }
1451