xref: /linux-6.15/lib/debugobjects.c (revision e18328ff)
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 
55*e18328ffSThomas Gleixner struct obj_pool {
56*e18328ffSThomas Gleixner 	struct hlist_head	objects;
57*e18328ffSThomas Gleixner 	unsigned int		cnt;
58*e18328ffSThomas Gleixner } ____cacheline_aligned;
59*e18328ffSThomas 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 
68*e18328ffSThomas Gleixner static struct obj_pool		pool_global;
69*e18328ffSThomas Gleixner static struct obj_pool		pool_to_free;
703ac7fe5aSThomas Gleixner 
71d86998b1SWaiman Long /*
72d86998b1SWaiman Long  * Because of the presence of percpu free pools, obj_pool_free will
73d86998b1SWaiman Long  * under-count those in the percpu free pools. Similarly, obj_pool_used
74d86998b1SWaiman Long  * will over-count those in the percpu free pools. Adjustments will be
75d86998b1SWaiman Long  * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
76d86998b1SWaiman Long  * can be off.
77d86998b1SWaiman Long  */
78e4757c71SZhen Lei static int __data_racy		obj_pool_min_free = ODEBUG_POOL_SIZE;
793ac7fe5aSThomas Gleixner static int			obj_pool_used;
80e4757c71SZhen Lei static int __data_racy		obj_pool_max_used;
81a7344a68SWaiman Long static bool			obj_freeing;
823ac7fe5aSThomas Gleixner 
835b5baba6SBreno Leitao static int __data_racy			debug_objects_maxchain __read_mostly;
845b5baba6SBreno Leitao static int __data_racy __maybe_unused	debug_objects_maxchecked __read_mostly;
855b5baba6SBreno Leitao static int __data_racy			debug_objects_fixups __read_mostly;
865b5baba6SBreno Leitao static int __data_racy			debug_objects_warnings __read_mostly;
87661cc28bSThomas Gleixner static bool __data_racy			debug_objects_enabled __read_mostly
883ae70205SIngo Molnar 					= CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
89e4757c71SZhen Lei static int				debug_objects_pool_size __ro_after_init
9097dd552eSWaiman Long 					= ODEBUG_POOL_SIZE;
91e4757c71SZhen Lei static int				debug_objects_pool_min_level __ro_after_init
9297dd552eSWaiman Long 					= ODEBUG_POOL_MIN_LEVEL;
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 
129*e18328ffSThomas Gleixner static __always_inline unsigned int pool_count(struct obj_pool *pool)
130*e18328ffSThomas Gleixner {
131*e18328ffSThomas Gleixner 	return READ_ONCE(pool->cnt);
132*e18328ffSThomas Gleixner }
133*e18328ffSThomas Gleixner 
134*e18328ffSThomas Gleixner static inline bool pool_global_should_refill(void)
135*e18328ffSThomas Gleixner {
136*e18328ffSThomas Gleixner 	return READ_ONCE(pool_global.cnt) < debug_objects_pool_min_level;
137*e18328ffSThomas Gleixner }
138*e18328ffSThomas Gleixner 
139*e18328ffSThomas Gleixner static inline bool pool_global_must_refill(void)
140*e18328ffSThomas Gleixner {
141*e18328ffSThomas Gleixner 	return READ_ONCE(pool_global.cnt) < (debug_objects_pool_min_level / 2);
142*e18328ffSThomas Gleixner }
143*e18328ffSThomas 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 	 */
167*e18328ffSThomas 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*e18328ffSThomas Gleixner 	while (pool_to_free.cnt && (pool_global.cnt < debug_objects_pool_min_level)) {
189*e18328ffSThomas Gleixner 		obj = hlist_entry(pool_to_free.objects.first, typeof(*obj), node);
19036c4ead6SYang Shi 		hlist_del(&obj->node);
191*e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt - 1);
192*e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_global.objects);
193*e18328ffSThomas 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*e18328ffSThomas Gleixner 	if (!pool_global_must_refill() && atomic_read(&cpus_allocating))
2081fda107dSThomas Gleixner 		return;
2093ac7fe5aSThomas Gleixner 
210d8c6cd3aSZhen Lei 	atomic_inc(&cpus_allocating);
211*e18328ffSThomas Gleixner 	while (pool_global_should_refill()) {
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);
228*e18328ffSThomas Gleixner 		hlist_splice_init(&head, &last->node, &pool_global.objects);
229813fd078SZhen Lei 		debug_objects_allocated += cnt;
230*e18328ffSThomas 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 {
272634d61f4SWaiman Long 	struct debug_percpu_free *percpu_pool = this_cpu_ptr(&percpu_obj_pool);
273d86998b1SWaiman Long 	struct debug_obj *obj;
274d86998b1SWaiman Long 
275d86998b1SWaiman Long 	if (likely(obj_cache)) {
276d86998b1SWaiman Long 		obj = __alloc_object(&percpu_pool->free_objs);
277d86998b1SWaiman Long 		if (obj) {
278d86998b1SWaiman Long 			percpu_pool->obj_free--;
279d86998b1SWaiman Long 			goto init_obj;
280d86998b1SWaiman Long 		}
281d86998b1SWaiman Long 	}
2823ac7fe5aSThomas Gleixner 
283aef9cb05SThomas Gleixner 	raw_spin_lock(&pool_lock);
284*e18328ffSThomas Gleixner 	obj = __alloc_object(&pool_global.objects);
285d86998b1SWaiman Long 	if (obj) {
2863ac7fe5aSThomas Gleixner 		obj_pool_used++;
287*e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt - 1);
288634d61f4SWaiman Long 
289634d61f4SWaiman Long 		/*
290634d61f4SWaiman Long 		 * Looking ahead, allocate one batch of debug objects and
291634d61f4SWaiman Long 		 * put them into the percpu free pool.
292634d61f4SWaiman Long 		 */
293634d61f4SWaiman Long 		if (likely(obj_cache)) {
294634d61f4SWaiman Long 			int i;
295634d61f4SWaiman Long 
296634d61f4SWaiman Long 			for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
297634d61f4SWaiman Long 				struct debug_obj *obj2;
298634d61f4SWaiman Long 
299*e18328ffSThomas Gleixner 				obj2 = __alloc_object(&pool_global.objects);
300634d61f4SWaiman Long 				if (!obj2)
301634d61f4SWaiman Long 					break;
302*e18328ffSThomas Gleixner 				hlist_add_head(&obj2->node, &percpu_pool->free_objs);
303634d61f4SWaiman Long 				percpu_pool->obj_free++;
304634d61f4SWaiman Long 				obj_pool_used++;
305*e18328ffSThomas Gleixner 				WRITE_ONCE(pool_global.cnt, pool_global.cnt - 1);
306634d61f4SWaiman Long 			}
307634d61f4SWaiman Long 		}
308634d61f4SWaiman Long 
3093ac7fe5aSThomas Gleixner 		if (obj_pool_used > obj_pool_max_used)
3103ac7fe5aSThomas Gleixner 			obj_pool_max_used = obj_pool_used;
3113ac7fe5aSThomas Gleixner 
312*e18328ffSThomas Gleixner 		if (pool_global.cnt < obj_pool_min_free)
313*e18328ffSThomas Gleixner 			obj_pool_min_free = pool_global.cnt;
3143ac7fe5aSThomas Gleixner 	}
315aef9cb05SThomas Gleixner 	raw_spin_unlock(&pool_lock);
3163ac7fe5aSThomas Gleixner 
317d86998b1SWaiman Long init_obj:
318d86998b1SWaiman Long 	if (obj) {
319d86998b1SWaiman Long 		obj->object = addr;
320d86998b1SWaiman Long 		obj->descr  = descr;
321d86998b1SWaiman Long 		obj->state  = ODEBUG_STATE_NONE;
322d86998b1SWaiman Long 		obj->astate = 0;
323d86998b1SWaiman Long 		hlist_add_head(&obj->node, &b->list);
324d86998b1SWaiman Long 	}
3253ac7fe5aSThomas Gleixner 	return obj;
3263ac7fe5aSThomas Gleixner }
3273ac7fe5aSThomas Gleixner 
3283ac7fe5aSThomas Gleixner /*
329337fff8bSThomas Gleixner  * workqueue function to free objects.
330858274b6SWaiman Long  *
331858274b6SWaiman Long  * To reduce contention on the global pool_lock, the actual freeing of
332636e1970SYang Shi  * debug objects will be delayed if the pool_lock is busy.
333337fff8bSThomas Gleixner  */
334337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work)
335337fff8bSThomas Gleixner {
33636c4ead6SYang Shi 	struct debug_obj *obj;
337337fff8bSThomas Gleixner 	unsigned long flags;
33836c4ead6SYang Shi 	HLIST_HEAD(tofree);
339337fff8bSThomas Gleixner 
340a7344a68SWaiman Long 	WRITE_ONCE(obj_freeing, false);
341858274b6SWaiman Long 	if (!raw_spin_trylock_irqsave(&pool_lock, flags))
342858274b6SWaiman Long 		return;
34336c4ead6SYang Shi 
344*e18328ffSThomas Gleixner 	if (pool_global.cnt >= debug_objects_pool_size)
345a7344a68SWaiman Long 		goto free_objs;
346a7344a68SWaiman Long 
34736c4ead6SYang Shi 	/*
34836c4ead6SYang Shi 	 * The objs on the pool list might be allocated before the work is
34936c4ead6SYang Shi 	 * run, so recheck if pool list it full or not, if not fill pool
350a7344a68SWaiman Long 	 * list from the global free list. As it is likely that a workload
351a7344a68SWaiman Long 	 * may be gearing up to use more and more objects, don't free any
352a7344a68SWaiman Long 	 * of them until the next round.
35336c4ead6SYang Shi 	 */
354*e18328ffSThomas Gleixner 	while (pool_to_free.cnt && pool_global.cnt < debug_objects_pool_size) {
355*e18328ffSThomas Gleixner 		obj = hlist_entry(pool_to_free.objects.first, typeof(*obj), node);
35636c4ead6SYang Shi 		hlist_del(&obj->node);
357*e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_global.objects);
358*e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt - 1);
359*e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + 1);
36036c4ead6SYang Shi 	}
361a7344a68SWaiman Long 	raw_spin_unlock_irqrestore(&pool_lock, flags);
362a7344a68SWaiman Long 	return;
36336c4ead6SYang Shi 
364a7344a68SWaiman Long free_objs:
36536c4ead6SYang Shi 	/*
36636c4ead6SYang Shi 	 * Pool list is already full and there are still objs on the free
36736c4ead6SYang Shi 	 * list. Move remaining free objs to a temporary list to free the
36836c4ead6SYang Shi 	 * memory outside the pool_lock held region.
36936c4ead6SYang Shi 	 */
370*e18328ffSThomas Gleixner 	if (pool_to_free.cnt) {
371*e18328ffSThomas Gleixner 		hlist_move_list(&pool_to_free.objects, &tofree);
372*e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, 0);
37336c4ead6SYang Shi 	}
374aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&pool_lock, flags);
37536c4ead6SYang Shi 
37649a5cb82SThomas Gleixner 	free_object_list(&tofree);
377337fff8bSThomas Gleixner }
378337fff8bSThomas Gleixner 
379a7344a68SWaiman Long static void __free_object(struct debug_obj *obj)
380636e1970SYang Shi {
381634d61f4SWaiman Long 	struct debug_obj *objs[ODEBUG_BATCH_SIZE];
382634d61f4SWaiman Long 	struct debug_percpu_free *percpu_pool;
383634d61f4SWaiman Long 	int lookahead_count = 0;
384636e1970SYang Shi 	unsigned long flags;
385636e1970SYang Shi 	bool work;
386636e1970SYang Shi 
387d86998b1SWaiman Long 	local_irq_save(flags);
388634d61f4SWaiman Long 	if (!obj_cache)
389634d61f4SWaiman Long 		goto free_to_obj_pool;
390634d61f4SWaiman Long 
391d86998b1SWaiman Long 	/*
392d86998b1SWaiman Long 	 * Try to free it into the percpu pool first.
393d86998b1SWaiman Long 	 */
394d86998b1SWaiman Long 	percpu_pool = this_cpu_ptr(&percpu_obj_pool);
395634d61f4SWaiman Long 	if (percpu_pool->obj_free < ODEBUG_POOL_PERCPU_SIZE) {
396d86998b1SWaiman Long 		hlist_add_head(&obj->node, &percpu_pool->free_objs);
397d86998b1SWaiman Long 		percpu_pool->obj_free++;
398d86998b1SWaiman Long 		local_irq_restore(flags);
399a7344a68SWaiman Long 		return;
400d86998b1SWaiman Long 	}
401d86998b1SWaiman Long 
402634d61f4SWaiman Long 	/*
403634d61f4SWaiman Long 	 * As the percpu pool is full, look ahead and pull out a batch
404634d61f4SWaiman Long 	 * of objects from the percpu pool and free them as well.
405634d61f4SWaiman Long 	 */
406634d61f4SWaiman Long 	for (; lookahead_count < ODEBUG_BATCH_SIZE; lookahead_count++) {
407634d61f4SWaiman Long 		objs[lookahead_count] = __alloc_object(&percpu_pool->free_objs);
408634d61f4SWaiman Long 		if (!objs[lookahead_count])
409634d61f4SWaiman Long 			break;
410634d61f4SWaiman Long 		percpu_pool->obj_free--;
411634d61f4SWaiman Long 	}
412634d61f4SWaiman Long 
413634d61f4SWaiman Long free_to_obj_pool:
414d86998b1SWaiman Long 	raw_spin_lock(&pool_lock);
415*e18328ffSThomas Gleixner 	work = (pool_global.cnt > debug_objects_pool_size) && obj_cache &&
416*e18328ffSThomas Gleixner 	       (pool_to_free.cnt < ODEBUG_FREE_WORK_MAX);
417636e1970SYang Shi 	obj_pool_used--;
418636e1970SYang Shi 
419636e1970SYang Shi 	if (work) {
420*e18328ffSThomas Gleixner 		WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt + 1);
421*e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_to_free.objects);
422634d61f4SWaiman Long 		if (lookahead_count) {
423*e18328ffSThomas Gleixner 			WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt + lookahead_count);
424634d61f4SWaiman Long 			obj_pool_used -= lookahead_count;
425634d61f4SWaiman Long 			while (lookahead_count) {
426634d61f4SWaiman Long 				hlist_add_head(&objs[--lookahead_count]->node,
427*e18328ffSThomas Gleixner 					       &pool_to_free.objects);
428634d61f4SWaiman Long 			}
429634d61f4SWaiman Long 		}
430a7344a68SWaiman Long 
431*e18328ffSThomas Gleixner 		if ((pool_global.cnt > debug_objects_pool_size) &&
432*e18328ffSThomas Gleixner 		    (pool_to_free.cnt < ODEBUG_FREE_WORK_MAX)) {
433a7344a68SWaiman Long 			int i;
434a7344a68SWaiman Long 
435a7344a68SWaiman Long 			/*
436a7344a68SWaiman Long 			 * Free one more batch of objects from obj_pool.
437a7344a68SWaiman Long 			 */
438a7344a68SWaiman Long 			for (i = 0; i < ODEBUG_BATCH_SIZE; i++) {
439*e18328ffSThomas Gleixner 				obj = __alloc_object(&pool_global.objects);
440*e18328ffSThomas Gleixner 				hlist_add_head(&obj->node, &pool_to_free.objects);
441*e18328ffSThomas Gleixner 				WRITE_ONCE(pool_global.cnt, pool_global.cnt - 1);
442*e18328ffSThomas Gleixner 				WRITE_ONCE(pool_to_free.cnt, pool_to_free.cnt + 1);
443a7344a68SWaiman Long 			}
444a7344a68SWaiman Long 		}
445636e1970SYang Shi 	} else {
446*e18328ffSThomas Gleixner 		WRITE_ONCE(pool_global.cnt, pool_global.cnt + 1);
447*e18328ffSThomas Gleixner 		hlist_add_head(&obj->node, &pool_global.objects);
448634d61f4SWaiman Long 		if (lookahead_count) {
449*e18328ffSThomas Gleixner 			WRITE_ONCE(pool_global.cnt, pool_global.cnt + lookahead_count);
450634d61f4SWaiman Long 			obj_pool_used -= lookahead_count;
451634d61f4SWaiman Long 			while (lookahead_count) {
452634d61f4SWaiman Long 				hlist_add_head(&objs[--lookahead_count]->node,
453*e18328ffSThomas Gleixner 					       &pool_global.objects);
454634d61f4SWaiman Long 			}
455634d61f4SWaiman Long 		}
456636e1970SYang Shi 	}
457d86998b1SWaiman Long 	raw_spin_unlock(&pool_lock);
458d86998b1SWaiman Long 	local_irq_restore(flags);
459636e1970SYang Shi }
460636e1970SYang Shi 
461337fff8bSThomas Gleixner /*
462337fff8bSThomas Gleixner  * Put the object back into the pool and schedule work to free objects
463337fff8bSThomas Gleixner  * if necessary.
4643ac7fe5aSThomas Gleixner  */
4653ac7fe5aSThomas Gleixner static void free_object(struct debug_obj *obj)
4663ac7fe5aSThomas Gleixner {
467a7344a68SWaiman Long 	__free_object(obj);
468*e18328ffSThomas Gleixner 	if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
469a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
470a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
471a7344a68SWaiman Long 	}
4723ac7fe5aSThomas Gleixner }
4733ac7fe5aSThomas Gleixner 
474a2a70238SThomas Gleixner static void put_objects(struct hlist_head *list)
47588451f2cSZqiang {
47688451f2cSZqiang 	struct hlist_node *tmp;
47788451f2cSZqiang 	struct debug_obj *obj;
47888451f2cSZqiang 
479a2a70238SThomas Gleixner 	/*
480a2a70238SThomas Gleixner 	 * Using free_object() puts the objects into reuse or schedules
481a2a70238SThomas Gleixner 	 * them for freeing and it get's all the accounting correct.
482a2a70238SThomas Gleixner 	 */
483a2a70238SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, list, node) {
48488451f2cSZqiang 		hlist_del(&obj->node);
485a2a70238SThomas Gleixner 		free_object(obj);
486a2a70238SThomas Gleixner 	}
48788451f2cSZqiang }
488eabb7f1aSwuchi 
48949968cf1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
490a2a70238SThomas Gleixner static int object_cpu_offline(unsigned int cpu)
491a2a70238SThomas Gleixner {
492a2a70238SThomas Gleixner 	/* Remote access is safe as the CPU is dead already */
493a2a70238SThomas Gleixner 	struct debug_percpu_free *pcp = per_cpu_ptr(&percpu_obj_pool, cpu);
494eabb7f1aSwuchi 
495a2a70238SThomas Gleixner 	put_objects(&pcp->free_objs);
496a2a70238SThomas Gleixner 	pcp->obj_free = 0;
49788451f2cSZqiang 	return 0;
49888451f2cSZqiang }
49988451f2cSZqiang #endif
50088451f2cSZqiang 
50149968cf1SThomas Gleixner /* Out of memory. Free all objects from hash */
5023ac7fe5aSThomas Gleixner static void debug_objects_oom(void)
5033ac7fe5aSThomas Gleixner {
5043ac7fe5aSThomas Gleixner 	struct debug_bucket *db = obj_hash;
505673d62ccSVegard Nossum 	HLIST_HEAD(freelist);
5063ac7fe5aSThomas Gleixner 
507719e4843SFabian Frederick 	pr_warn("Out of memory. ODEBUG disabled\n");
5083ac7fe5aSThomas Gleixner 
50949968cf1SThomas Gleixner 	for (int i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
51049968cf1SThomas Gleixner 		scoped_guard(raw_spinlock_irqsave, &db->lock)
511673d62ccSVegard Nossum 			hlist_move_list(&db->list, &freelist);
512673d62ccSVegard Nossum 
51349968cf1SThomas Gleixner 		put_objects(&freelist);
5143ac7fe5aSThomas Gleixner 	}
5153ac7fe5aSThomas Gleixner }
5163ac7fe5aSThomas Gleixner 
5173ac7fe5aSThomas Gleixner /*
5183ac7fe5aSThomas Gleixner  * We use the pfn of the address for the hash. That way we can check
5193ac7fe5aSThomas Gleixner  * for freed objects simply by checking the affected bucket.
5203ac7fe5aSThomas Gleixner  */
5213ac7fe5aSThomas Gleixner static struct debug_bucket *get_bucket(unsigned long addr)
5223ac7fe5aSThomas Gleixner {
5233ac7fe5aSThomas Gleixner 	unsigned long hash;
5243ac7fe5aSThomas Gleixner 
5253ac7fe5aSThomas Gleixner 	hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
5263ac7fe5aSThomas Gleixner 	return &obj_hash[hash];
5273ac7fe5aSThomas Gleixner }
5283ac7fe5aSThomas Gleixner 
5293ac7fe5aSThomas Gleixner static void debug_print_object(struct debug_obj *obj, char *msg)
5303ac7fe5aSThomas Gleixner {
531aedcade6SStephen Boyd 	const struct debug_obj_descr *descr = obj->descr;
5323ac7fe5aSThomas Gleixner 	static int limit;
5333ac7fe5aSThomas Gleixner 
5348b64d420STetsuo Handa 	/*
5358b64d420STetsuo Handa 	 * Don't report if lookup_object_or_alloc() by the current thread
5368b64d420STetsuo Handa 	 * failed because lookup_object_or_alloc()/debug_objects_oom() by a
5378b64d420STetsuo Handa 	 * concurrent thread turned off debug_objects_enabled and cleared
5388b64d420STetsuo Handa 	 * the hash buckets.
5398b64d420STetsuo Handa 	 */
5408b64d420STetsuo Handa 	if (!debug_objects_enabled)
5418b64d420STetsuo Handa 		return;
5428b64d420STetsuo Handa 
54399777288SStanislaw Gruszka 	if (limit < 5 && descr != descr_test) {
54499777288SStanislaw Gruszka 		void *hint = descr->debug_hint ?
54599777288SStanislaw Gruszka 			descr->debug_hint(obj->object) : NULL;
5463ac7fe5aSThomas Gleixner 		limit++;
547a5d8e467SMathieu Desnoyers 		WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
548c4db2d3bSStephen Boyd 				 "object: %p object type: %s hint: %pS\n",
549a5d8e467SMathieu Desnoyers 			msg, obj_states[obj->state], obj->astate,
550c4db2d3bSStephen Boyd 			obj->object, descr->name, hint);
5513ac7fe5aSThomas Gleixner 	}
5523ac7fe5aSThomas Gleixner 	debug_objects_warnings++;
5533ac7fe5aSThomas Gleixner }
5543ac7fe5aSThomas Gleixner 
5553ac7fe5aSThomas Gleixner /*
5563ac7fe5aSThomas Gleixner  * Try to repair the damage, so we have a better chance to get useful
5573ac7fe5aSThomas Gleixner  * debug output.
5583ac7fe5aSThomas Gleixner  */
559b1e4d9d8SDu, Changbin static bool
560b1e4d9d8SDu, Changbin debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
5613ac7fe5aSThomas Gleixner 		   void * addr, enum debug_obj_state state)
5623ac7fe5aSThomas Gleixner {
563b1e4d9d8SDu, Changbin 	if (fixup && fixup(addr, state)) {
564b1e4d9d8SDu, Changbin 		debug_objects_fixups++;
565b1e4d9d8SDu, Changbin 		return true;
566b1e4d9d8SDu, Changbin 	}
567b1e4d9d8SDu, Changbin 	return false;
5683ac7fe5aSThomas Gleixner }
5693ac7fe5aSThomas Gleixner 
5703ac7fe5aSThomas Gleixner static void debug_object_is_on_stack(void *addr, int onstack)
5713ac7fe5aSThomas Gleixner {
5723ac7fe5aSThomas Gleixner 	int is_on_stack;
5733ac7fe5aSThomas Gleixner 	static int limit;
5743ac7fe5aSThomas Gleixner 
5753ac7fe5aSThomas Gleixner 	if (limit > 4)
5763ac7fe5aSThomas Gleixner 		return;
5773ac7fe5aSThomas Gleixner 
5788b05c7e6SFUJITA Tomonori 	is_on_stack = object_is_on_stack(addr);
5793ac7fe5aSThomas Gleixner 	if (is_on_stack == onstack)
5803ac7fe5aSThomas Gleixner 		return;
5813ac7fe5aSThomas Gleixner 
5823ac7fe5aSThomas Gleixner 	limit++;
5833ac7fe5aSThomas Gleixner 	if (is_on_stack)
584fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
585fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
5863ac7fe5aSThomas Gleixner 	else
587fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
588fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
589fc91a3c4SJoel Fernandes (Google) 
5903ac7fe5aSThomas Gleixner 	WARN_ON(1);
5913ac7fe5aSThomas Gleixner }
5923ac7fe5aSThomas Gleixner 
59363a75969SThomas Gleixner static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
59463a75969SThomas Gleixner 						const struct debug_obj_descr *descr,
59563a75969SThomas Gleixner 						bool onstack, bool alloc_ifstatic)
59663a75969SThomas Gleixner {
59763a75969SThomas Gleixner 	struct debug_obj *obj = lookup_object(addr, b);
59863a75969SThomas Gleixner 	enum debug_obj_state state = ODEBUG_STATE_NONE;
59963a75969SThomas Gleixner 
60063a75969SThomas Gleixner 	if (likely(obj))
60163a75969SThomas Gleixner 		return obj;
60263a75969SThomas Gleixner 
60363a75969SThomas Gleixner 	/*
60463a75969SThomas Gleixner 	 * debug_object_init() unconditionally allocates untracked
60563a75969SThomas Gleixner 	 * objects. It does not matter whether it is a static object or
60663a75969SThomas Gleixner 	 * not.
60763a75969SThomas Gleixner 	 *
60863a75969SThomas Gleixner 	 * debug_object_assert_init() and debug_object_activate() allow
60963a75969SThomas Gleixner 	 * allocation only if the descriptor callback confirms that the
61063a75969SThomas Gleixner 	 * object is static and considered initialized. For non-static
61163a75969SThomas Gleixner 	 * objects the allocation needs to be done from the fixup callback.
61263a75969SThomas Gleixner 	 */
61363a75969SThomas Gleixner 	if (unlikely(alloc_ifstatic)) {
61463a75969SThomas Gleixner 		if (!descr->is_static_object || !descr->is_static_object(addr))
61563a75969SThomas Gleixner 			return ERR_PTR(-ENOENT);
61663a75969SThomas Gleixner 		/* Statically allocated objects are considered initialized */
61763a75969SThomas Gleixner 		state = ODEBUG_STATE_INIT;
61863a75969SThomas Gleixner 	}
61963a75969SThomas Gleixner 
62063a75969SThomas Gleixner 	obj = alloc_object(addr, b, descr);
62163a75969SThomas Gleixner 	if (likely(obj)) {
62263a75969SThomas Gleixner 		obj->state = state;
62363a75969SThomas Gleixner 		debug_object_is_on_stack(addr, onstack);
62463a75969SThomas Gleixner 		return obj;
62563a75969SThomas Gleixner 	}
62663a75969SThomas Gleixner 
62763a75969SThomas Gleixner 	/* Out of memory. Do the cleanup outside of the locked region */
628661cc28bSThomas Gleixner 	debug_objects_enabled = false;
62963a75969SThomas Gleixner 	return NULL;
63063a75969SThomas Gleixner }
63163a75969SThomas Gleixner 
6320af462f1SThomas Gleixner static void debug_objects_fill_pool(void)
6330af462f1SThomas Gleixner {
634d8c6cd3aSZhen Lei 	if (unlikely(!obj_cache))
635d8c6cd3aSZhen Lei 		return;
636d8c6cd3aSZhen Lei 
637*e18328ffSThomas Gleixner 	if (likely(!pool_global_should_refill()))
638d8c6cd3aSZhen Lei 		return;
639d8c6cd3aSZhen Lei 
640d8c6cd3aSZhen Lei 	/* Try reusing objects from obj_to_free_list */
641d8c6cd3aSZhen Lei 	fill_pool_from_freelist();
642d8c6cd3aSZhen Lei 
643*e18328ffSThomas Gleixner 	if (likely(!pool_global_should_refill()))
644d8c6cd3aSZhen Lei 		return;
645d8c6cd3aSZhen Lei 
6460af462f1SThomas Gleixner 	/*
6470af462f1SThomas Gleixner 	 * On RT enabled kernels the pool refill must happen in preemptible
6480cce06baSPeter Zijlstra 	 * context -- for !RT kernels we rely on the fact that spinlock_t and
6490cce06baSPeter Zijlstra 	 * raw_spinlock_t are basically the same type and this lock-type
6500cce06baSPeter Zijlstra 	 * inversion works just fine.
6510af462f1SThomas Gleixner 	 */
6520cce06baSPeter Zijlstra 	if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible()) {
6530cce06baSPeter Zijlstra 		/*
6540cce06baSPeter Zijlstra 		 * Annotate away the spinlock_t inside raw_spinlock_t warning
6550cce06baSPeter Zijlstra 		 * by temporarily raising the wait-type to WAIT_SLEEP, matching
6560cce06baSPeter Zijlstra 		 * the preemptible() condition above.
6570cce06baSPeter Zijlstra 		 */
6580cce06baSPeter Zijlstra 		static DEFINE_WAIT_OVERRIDE_MAP(fill_pool_map, LD_WAIT_SLEEP);
6590cce06baSPeter Zijlstra 		lock_map_acquire_try(&fill_pool_map);
6600af462f1SThomas Gleixner 		fill_pool();
6610cce06baSPeter Zijlstra 		lock_map_release(&fill_pool_map);
6620cce06baSPeter Zijlstra 	}
6630af462f1SThomas Gleixner }
6640af462f1SThomas Gleixner 
6653ac7fe5aSThomas Gleixner static void
666aedcade6SStephen Boyd __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
6673ac7fe5aSThomas Gleixner {
6689bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
6693ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
6703ac7fe5aSThomas Gleixner 	unsigned long flags;
6713ac7fe5aSThomas Gleixner 
6720af462f1SThomas Gleixner 	debug_objects_fill_pool();
67350db04ddSVegard Nossum 
6743ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
6753ac7fe5aSThomas Gleixner 
676aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
6773ac7fe5aSThomas Gleixner 
67863a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
67963a75969SThomas Gleixner 	if (unlikely(!obj)) {
680aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
6813ac7fe5aSThomas Gleixner 		debug_objects_oom();
6823ac7fe5aSThomas Gleixner 		return;
6833ac7fe5aSThomas Gleixner 	}
6843ac7fe5aSThomas Gleixner 
6853ac7fe5aSThomas Gleixner 	switch (obj->state) {
6863ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
6873ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
6883ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
6893ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_INIT;
690aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
691d5f34153SWaiman Long 		return;
6923ac7fe5aSThomas Gleixner 	default:
6933ac7fe5aSThomas Gleixner 		break;
6943ac7fe5aSThomas Gleixner 	}
6953ac7fe5aSThomas Gleixner 
6969bb63626SAndrzej Hajda 	o = *obj;
697aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
6989bb63626SAndrzej Hajda 	debug_print_object(&o, "init");
6999bb63626SAndrzej Hajda 
7009bb63626SAndrzej Hajda 	if (o.state == ODEBUG_STATE_ACTIVE)
7019bb63626SAndrzej Hajda 		debug_object_fixup(descr->fixup_init, addr, o.state);
7023ac7fe5aSThomas Gleixner }
7033ac7fe5aSThomas Gleixner 
7043ac7fe5aSThomas Gleixner /**
7053ac7fe5aSThomas Gleixner  * debug_object_init - debug checks when an object is initialized
7063ac7fe5aSThomas Gleixner  * @addr:	address of the object
7073ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7083ac7fe5aSThomas Gleixner  */
709aedcade6SStephen Boyd void debug_object_init(void *addr, const struct debug_obj_descr *descr)
7103ac7fe5aSThomas Gleixner {
7113ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7123ac7fe5aSThomas Gleixner 		return;
7133ac7fe5aSThomas Gleixner 
7143ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 0);
7153ac7fe5aSThomas Gleixner }
716f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init);
7173ac7fe5aSThomas Gleixner 
7183ac7fe5aSThomas Gleixner /**
7193ac7fe5aSThomas Gleixner  * debug_object_init_on_stack - debug checks when an object on stack is
7203ac7fe5aSThomas Gleixner  *				initialized
7213ac7fe5aSThomas Gleixner  * @addr:	address of the object
7223ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7233ac7fe5aSThomas Gleixner  */
724aedcade6SStephen Boyd void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr)
7253ac7fe5aSThomas Gleixner {
7263ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7273ac7fe5aSThomas Gleixner 		return;
7283ac7fe5aSThomas Gleixner 
7293ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 1);
7303ac7fe5aSThomas Gleixner }
731f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
7323ac7fe5aSThomas Gleixner 
7333ac7fe5aSThomas Gleixner /**
7343ac7fe5aSThomas Gleixner  * debug_object_activate - debug checks when an object is activated
7353ac7fe5aSThomas Gleixner  * @addr:	address of the object
7363ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
737b778ae25SPaul E. McKenney  * Returns 0 for success, -EINVAL for check failed.
7383ac7fe5aSThomas Gleixner  */
739aedcade6SStephen Boyd int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
7403ac7fe5aSThomas Gleixner {
74163a75969SThomas Gleixner 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
7423ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
7433ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
7443ac7fe5aSThomas Gleixner 	unsigned long flags;
7453ac7fe5aSThomas Gleixner 
7463ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
747b778ae25SPaul E. McKenney 		return 0;
7483ac7fe5aSThomas Gleixner 
7490af462f1SThomas Gleixner 	debug_objects_fill_pool();
7500af462f1SThomas Gleixner 
7513ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
7523ac7fe5aSThomas Gleixner 
753aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
7543ac7fe5aSThomas Gleixner 
75563a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, false, true);
7569bb63626SAndrzej Hajda 	if (unlikely(!obj)) {
7579bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
7589bb63626SAndrzej Hajda 		debug_objects_oom();
7599bb63626SAndrzej Hajda 		return 0;
7609bb63626SAndrzej Hajda 	} else if (likely(!IS_ERR(obj))) {
7613ac7fe5aSThomas Gleixner 		switch (obj->state) {
7629bb63626SAndrzej Hajda 		case ODEBUG_STATE_ACTIVE:
7639bb63626SAndrzej Hajda 		case ODEBUG_STATE_DESTROYED:
7649bb63626SAndrzej Hajda 			o = *obj;
7659bb63626SAndrzej Hajda 			break;
7663ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
7673ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
7683ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_ACTIVE;
7699bb63626SAndrzej Hajda 			fallthrough;
7703ac7fe5aSThomas Gleixner 		default:
771aef9cb05SThomas Gleixner 			raw_spin_unlock_irqrestore(&db->lock, flags);
772b778ae25SPaul E. McKenney 			return 0;
7733ac7fe5aSThomas Gleixner 		}
7749bb63626SAndrzej Hajda 	}
77563a75969SThomas Gleixner 
7769bb63626SAndrzej Hajda 	raw_spin_unlock_irqrestore(&db->lock, flags);
77763a75969SThomas Gleixner 	debug_print_object(&o, "activate");
7789bb63626SAndrzej Hajda 
7799bb63626SAndrzej Hajda 	switch (o.state) {
7809bb63626SAndrzej Hajda 	case ODEBUG_STATE_ACTIVE:
7819bb63626SAndrzej Hajda 	case ODEBUG_STATE_NOTAVAILABLE:
7829bb63626SAndrzej Hajda 		if (debug_object_fixup(descr->fixup_activate, addr, o.state))
7839bb63626SAndrzej Hajda 			return 0;
7849bb63626SAndrzej Hajda 		fallthrough;
7859bb63626SAndrzej Hajda 	default:
7869bb63626SAndrzej Hajda 		return -EINVAL;
7879bb63626SAndrzej Hajda 	}
78863a75969SThomas Gleixner }
789f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_activate);
7903ac7fe5aSThomas Gleixner 
7913ac7fe5aSThomas Gleixner /**
7923ac7fe5aSThomas Gleixner  * debug_object_deactivate - debug checks when an object is deactivated
7933ac7fe5aSThomas Gleixner  * @addr:	address of the object
7943ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7953ac7fe5aSThomas Gleixner  */
796aedcade6SStephen Boyd void debug_object_deactivate(void *addr, const struct debug_obj_descr *descr)
7973ac7fe5aSThomas Gleixner {
7989bb63626SAndrzej Hajda 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
7993ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8003ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
8013ac7fe5aSThomas Gleixner 	unsigned long flags;
8023ac7fe5aSThomas Gleixner 
8033ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8043ac7fe5aSThomas Gleixner 		return;
8053ac7fe5aSThomas Gleixner 
8063ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8073ac7fe5aSThomas Gleixner 
808aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8093ac7fe5aSThomas Gleixner 
8103ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
8113ac7fe5aSThomas Gleixner 	if (obj) {
8123ac7fe5aSThomas Gleixner 		switch (obj->state) {
8139bb63626SAndrzej Hajda 		case ODEBUG_STATE_DESTROYED:
8149bb63626SAndrzej Hajda 			break;
8153ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
8163ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
8173ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_ACTIVE:
8189bb63626SAndrzej Hajda 			if (obj->astate)
8199bb63626SAndrzej Hajda 				break;
8203ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_INACTIVE;
8219bb63626SAndrzej Hajda 			fallthrough;
8223ac7fe5aSThomas Gleixner 		default:
8239bb63626SAndrzej Hajda 			raw_spin_unlock_irqrestore(&db->lock, flags);
8249bb63626SAndrzej Hajda 			return;
8253ac7fe5aSThomas Gleixner 		}
8269bb63626SAndrzej Hajda 		o = *obj;
827d5f34153SWaiman Long 	}
828d5f34153SWaiman Long 
829d5f34153SWaiman Long 	raw_spin_unlock_irqrestore(&db->lock, flags);
8303ac7fe5aSThomas Gleixner 	debug_print_object(&o, "deactivate");
8313ac7fe5aSThomas Gleixner }
832f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_deactivate);
8333ac7fe5aSThomas Gleixner 
8343ac7fe5aSThomas Gleixner /**
8353ac7fe5aSThomas Gleixner  * debug_object_destroy - debug checks when an object is destroyed
8363ac7fe5aSThomas Gleixner  * @addr:	address of the object
8373ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
8383ac7fe5aSThomas Gleixner  */
839aedcade6SStephen Boyd void debug_object_destroy(void *addr, const struct debug_obj_descr *descr)
8403ac7fe5aSThomas Gleixner {
8419bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
8423ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8433ac7fe5aSThomas Gleixner 	unsigned long flags;
8443ac7fe5aSThomas Gleixner 
8453ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8463ac7fe5aSThomas Gleixner 		return;
8473ac7fe5aSThomas Gleixner 
8483ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8493ac7fe5aSThomas Gleixner 
850aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8513ac7fe5aSThomas Gleixner 
8523ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
8539bb63626SAndrzej Hajda 	if (!obj) {
8549bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
8559bb63626SAndrzej Hajda 		return;
8569bb63626SAndrzej Hajda 	}
8573ac7fe5aSThomas Gleixner 
8583ac7fe5aSThomas Gleixner 	switch (obj->state) {
8599bb63626SAndrzej Hajda 	case ODEBUG_STATE_ACTIVE:
8609bb63626SAndrzej Hajda 	case ODEBUG_STATE_DESTROYED:
8619bb63626SAndrzej Hajda 		break;
8623ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
8633ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
8643ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
8653ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_DESTROYED;
8669bb63626SAndrzej Hajda 		fallthrough;
8673ac7fe5aSThomas Gleixner 	default:
868aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
8699bb63626SAndrzej Hajda 		return;
8709bb63626SAndrzej Hajda 	}
8719bb63626SAndrzej Hajda 
8729bb63626SAndrzej Hajda 	o = *obj;
8739bb63626SAndrzej Hajda 	raw_spin_unlock_irqrestore(&db->lock, flags);
8749bb63626SAndrzej Hajda 	debug_print_object(&o, "destroy");
8759bb63626SAndrzej Hajda 
8769bb63626SAndrzej Hajda 	if (o.state == ODEBUG_STATE_ACTIVE)
8779bb63626SAndrzej Hajda 		debug_object_fixup(descr->fixup_destroy, addr, o.state);
8783ac7fe5aSThomas Gleixner }
879f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_destroy);
8803ac7fe5aSThomas Gleixner 
8813ac7fe5aSThomas Gleixner /**
8823ac7fe5aSThomas Gleixner  * debug_object_free - debug checks when an object is freed
8833ac7fe5aSThomas Gleixner  * @addr:	address of the object
8843ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
8853ac7fe5aSThomas Gleixner  */
886aedcade6SStephen Boyd void debug_object_free(void *addr, const struct debug_obj_descr *descr)
8873ac7fe5aSThomas Gleixner {
8889bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
8893ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8903ac7fe5aSThomas Gleixner 	unsigned long flags;
8913ac7fe5aSThomas Gleixner 
8923ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8933ac7fe5aSThomas Gleixner 		return;
8943ac7fe5aSThomas Gleixner 
8953ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8963ac7fe5aSThomas Gleixner 
897aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8983ac7fe5aSThomas Gleixner 
8993ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
9009bb63626SAndrzej Hajda 	if (!obj) {
9019bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
9029bb63626SAndrzej Hajda 		return;
9039bb63626SAndrzej Hajda 	}
9043ac7fe5aSThomas Gleixner 
9053ac7fe5aSThomas Gleixner 	switch (obj->state) {
9063ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
9079bb63626SAndrzej Hajda 		break;
9083ac7fe5aSThomas Gleixner 	default:
9093ac7fe5aSThomas Gleixner 		hlist_del(&obj->node);
910aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
9113ac7fe5aSThomas Gleixner 		free_object(obj);
912673d62ccSVegard Nossum 		return;
9133ac7fe5aSThomas Gleixner 	}
9149bb63626SAndrzej Hajda 
9159bb63626SAndrzej Hajda 	o = *obj;
916aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
9179bb63626SAndrzej Hajda 	debug_print_object(&o, "free");
9189bb63626SAndrzej Hajda 
9199bb63626SAndrzej Hajda 	debug_object_fixup(descr->fixup_free, addr, o.state);
9203ac7fe5aSThomas Gleixner }
921f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_free);
9223ac7fe5aSThomas Gleixner 
923a5d8e467SMathieu Desnoyers /**
924b84d435cSChristine Chan  * debug_object_assert_init - debug checks when object should be init-ed
925b84d435cSChristine Chan  * @addr:	address of the object
926b84d435cSChristine Chan  * @descr:	pointer to an object specific debug description structure
927b84d435cSChristine Chan  */
928aedcade6SStephen Boyd void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
929b84d435cSChristine Chan {
93063a75969SThomas Gleixner 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
931b84d435cSChristine Chan 	struct debug_bucket *db;
932b84d435cSChristine Chan 	struct debug_obj *obj;
933b84d435cSChristine Chan 	unsigned long flags;
934b84d435cSChristine Chan 
935b84d435cSChristine Chan 	if (!debug_objects_enabled)
936b84d435cSChristine Chan 		return;
937b84d435cSChristine Chan 
9380af462f1SThomas Gleixner 	debug_objects_fill_pool();
9390af462f1SThomas Gleixner 
940b84d435cSChristine Chan 	db = get_bucket((unsigned long) addr);
941b84d435cSChristine Chan 
942b84d435cSChristine Chan 	raw_spin_lock_irqsave(&db->lock, flags);
94363a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, false, true);
944b84d435cSChristine Chan 	raw_spin_unlock_irqrestore(&db->lock, flags);
94563a75969SThomas Gleixner 	if (likely(!IS_ERR_OR_NULL(obj)))
94663a75969SThomas Gleixner 		return;
94763a75969SThomas Gleixner 
94863a75969SThomas Gleixner 	/* If NULL the allocation has hit OOM */
94963a75969SThomas Gleixner 	if (!obj) {
95063a75969SThomas Gleixner 		debug_objects_oom();
951b84d435cSChristine Chan 		return;
952b84d435cSChristine Chan 	}
953b84d435cSChristine Chan 
95463a75969SThomas Gleixner 	/* Object is neither tracked nor static. It's not initialized. */
95563a75969SThomas Gleixner 	debug_print_object(&o, "assert_init");
95663a75969SThomas Gleixner 	debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
957b84d435cSChristine Chan }
958f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_assert_init);
959b84d435cSChristine Chan 
960b84d435cSChristine Chan /**
961a5d8e467SMathieu Desnoyers  * debug_object_active_state - debug checks object usage state machine
962a5d8e467SMathieu Desnoyers  * @addr:	address of the object
963a5d8e467SMathieu Desnoyers  * @descr:	pointer to an object specific debug description structure
964a5d8e467SMathieu Desnoyers  * @expect:	expected state
965a5d8e467SMathieu Desnoyers  * @next:	state to move to if expected state is found
966a5d8e467SMathieu Desnoyers  */
967a5d8e467SMathieu Desnoyers void
968aedcade6SStephen Boyd debug_object_active_state(void *addr, const struct debug_obj_descr *descr,
969a5d8e467SMathieu Desnoyers 			  unsigned int expect, unsigned int next)
970a5d8e467SMathieu Desnoyers {
9719bb63626SAndrzej Hajda 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
972a5d8e467SMathieu Desnoyers 	struct debug_bucket *db;
973a5d8e467SMathieu Desnoyers 	struct debug_obj *obj;
974a5d8e467SMathieu Desnoyers 	unsigned long flags;
975a5d8e467SMathieu Desnoyers 
976a5d8e467SMathieu Desnoyers 	if (!debug_objects_enabled)
977a5d8e467SMathieu Desnoyers 		return;
978a5d8e467SMathieu Desnoyers 
979a5d8e467SMathieu Desnoyers 	db = get_bucket((unsigned long) addr);
980a5d8e467SMathieu Desnoyers 
981a5d8e467SMathieu Desnoyers 	raw_spin_lock_irqsave(&db->lock, flags);
982a5d8e467SMathieu Desnoyers 
983a5d8e467SMathieu Desnoyers 	obj = lookup_object(addr, db);
984a5d8e467SMathieu Desnoyers 	if (obj) {
985a5d8e467SMathieu Desnoyers 		switch (obj->state) {
986a5d8e467SMathieu Desnoyers 		case ODEBUG_STATE_ACTIVE:
9879bb63626SAndrzej Hajda 			if (obj->astate != expect)
988a5d8e467SMathieu Desnoyers 				break;
9899bb63626SAndrzej Hajda 			obj->astate = next;
9909bb63626SAndrzej Hajda 			raw_spin_unlock_irqrestore(&db->lock, flags);
9919bb63626SAndrzej Hajda 			return;
992a5d8e467SMathieu Desnoyers 		default:
993a5d8e467SMathieu Desnoyers 			break;
994a5d8e467SMathieu Desnoyers 		}
9959bb63626SAndrzej Hajda 		o = *obj;
996d5f34153SWaiman Long 	}
997d5f34153SWaiman Long 
998d5f34153SWaiman Long 	raw_spin_unlock_irqrestore(&db->lock, flags);
999a5d8e467SMathieu Desnoyers 	debug_print_object(&o, "active_state");
1000a5d8e467SMathieu Desnoyers }
1001f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_active_state);
1002a5d8e467SMathieu Desnoyers 
10033ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
10043ac7fe5aSThomas Gleixner static void __debug_check_no_obj_freed(const void *address, unsigned long size)
10053ac7fe5aSThomas Gleixner {
10063ac7fe5aSThomas Gleixner 	unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
10079bb63626SAndrzej Hajda 	int cnt, objs_checked = 0;
10089bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
10093ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
10101ea9b98bSYang Shi 	struct hlist_node *tmp;
10113ac7fe5aSThomas Gleixner 
10123ac7fe5aSThomas Gleixner 	saddr = (unsigned long) address;
10133ac7fe5aSThomas Gleixner 	eaddr = saddr + size;
10143ac7fe5aSThomas Gleixner 	paddr = saddr & ODEBUG_CHUNK_MASK;
10153ac7fe5aSThomas Gleixner 	chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
10163ac7fe5aSThomas Gleixner 	chunks >>= ODEBUG_CHUNK_SHIFT;
10173ac7fe5aSThomas Gleixner 
10183ac7fe5aSThomas Gleixner 	for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
10193ac7fe5aSThomas Gleixner 		db = get_bucket(paddr);
10203ac7fe5aSThomas Gleixner 
10213ac7fe5aSThomas Gleixner repeat:
10223ac7fe5aSThomas Gleixner 		cnt = 0;
1023aef9cb05SThomas Gleixner 		raw_spin_lock_irqsave(&db->lock, flags);
1024b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
10253ac7fe5aSThomas Gleixner 			cnt++;
10263ac7fe5aSThomas Gleixner 			oaddr = (unsigned long) obj->object;
10273ac7fe5aSThomas Gleixner 			if (oaddr < saddr || oaddr >= eaddr)
10283ac7fe5aSThomas Gleixner 				continue;
10293ac7fe5aSThomas Gleixner 
10303ac7fe5aSThomas Gleixner 			switch (obj->state) {
10313ac7fe5aSThomas Gleixner 			case ODEBUG_STATE_ACTIVE:
10329bb63626SAndrzej Hajda 				o = *obj;
1033aef9cb05SThomas Gleixner 				raw_spin_unlock_irqrestore(&db->lock, flags);
10349bb63626SAndrzej Hajda 				debug_print_object(&o, "free");
10359bb63626SAndrzej Hajda 				debug_object_fixup(o.descr->fixup_free, (void *)oaddr, o.state);
10363ac7fe5aSThomas Gleixner 				goto repeat;
10373ac7fe5aSThomas Gleixner 			default:
10383ac7fe5aSThomas Gleixner 				hlist_del(&obj->node);
1039a7344a68SWaiman Long 				__free_object(obj);
10403ac7fe5aSThomas Gleixner 				break;
10413ac7fe5aSThomas Gleixner 			}
10423ac7fe5aSThomas Gleixner 		}
1043aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
1044673d62ccSVegard Nossum 
10453ac7fe5aSThomas Gleixner 		if (cnt > debug_objects_maxchain)
10463ac7fe5aSThomas Gleixner 			debug_objects_maxchain = cnt;
1047bd9dcd04SYang Shi 
1048bd9dcd04SYang Shi 		objs_checked += cnt;
10493ac7fe5aSThomas Gleixner 	}
1050bd9dcd04SYang Shi 
1051bd9dcd04SYang Shi 	if (objs_checked > debug_objects_maxchecked)
1052bd9dcd04SYang Shi 		debug_objects_maxchecked = objs_checked;
10531ea9b98bSYang Shi 
10541ea9b98bSYang Shi 	/* Schedule work to actually kmem_cache_free() objects */
1055*e18328ffSThomas Gleixner 	if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
1056a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
1057a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
1058a7344a68SWaiman Long 	}
10593ac7fe5aSThomas Gleixner }
10603ac7fe5aSThomas Gleixner 
10613ac7fe5aSThomas Gleixner void debug_check_no_obj_freed(const void *address, unsigned long size)
10623ac7fe5aSThomas Gleixner {
10633ac7fe5aSThomas Gleixner 	if (debug_objects_enabled)
10643ac7fe5aSThomas Gleixner 		__debug_check_no_obj_freed(address, size);
10653ac7fe5aSThomas Gleixner }
10663ac7fe5aSThomas Gleixner #endif
10673ac7fe5aSThomas Gleixner 
10683ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_FS
10693ac7fe5aSThomas Gleixner 
10703ac7fe5aSThomas Gleixner static int debug_stats_show(struct seq_file *m, void *v)
10713ac7fe5aSThomas Gleixner {
1072d86998b1SWaiman Long 	int cpu, obj_percpu_free = 0;
1073d86998b1SWaiman Long 
1074d86998b1SWaiman Long 	for_each_possible_cpu(cpu)
1075d86998b1SWaiman Long 		obj_percpu_free += per_cpu(percpu_obj_pool.obj_free, cpu);
1076d86998b1SWaiman Long 
10773ac7fe5aSThomas Gleixner 	seq_printf(m, "max_chain     :%d\n", debug_objects_maxchain);
1078bd9dcd04SYang Shi 	seq_printf(m, "max_checked   :%d\n", debug_objects_maxchecked);
10793ac7fe5aSThomas Gleixner 	seq_printf(m, "warnings      :%d\n", debug_objects_warnings);
10803ac7fe5aSThomas Gleixner 	seq_printf(m, "fixups        :%d\n", debug_objects_fixups);
1081*e18328ffSThomas Gleixner 	seq_printf(m, "pool_free     :%d\n", pool_count(&pool_global) + obj_percpu_free);
1082d86998b1SWaiman Long 	seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
10833ac7fe5aSThomas Gleixner 	seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
1084d86998b1SWaiman Long 	seq_printf(m, "pool_used     :%d\n", obj_pool_used - obj_percpu_free);
10853ac7fe5aSThomas Gleixner 	seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
1086*e18328ffSThomas Gleixner 	seq_printf(m, "on_free_list  :%d\n", pool_count(&pool_to_free));
10870cad93c3SWaiman Long 	seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
10880cad93c3SWaiman Long 	seq_printf(m, "objs_freed    :%d\n", debug_objects_freed);
10893ac7fe5aSThomas Gleixner 	return 0;
10903ac7fe5aSThomas Gleixner }
10910f85c480SQinglang Miao DEFINE_SHOW_ATTRIBUTE(debug_stats);
10923ac7fe5aSThomas Gleixner 
10933ac7fe5aSThomas Gleixner static int __init debug_objects_init_debugfs(void)
10943ac7fe5aSThomas Gleixner {
1095fecb0d95SGreg Kroah-Hartman 	struct dentry *dbgdir;
10963ac7fe5aSThomas Gleixner 
10973ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
10983ac7fe5aSThomas Gleixner 		return 0;
10993ac7fe5aSThomas Gleixner 
11003ac7fe5aSThomas Gleixner 	dbgdir = debugfs_create_dir("debug_objects", NULL);
11013ac7fe5aSThomas Gleixner 
1102fecb0d95SGreg Kroah-Hartman 	debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
11033ac7fe5aSThomas Gleixner 
11043ac7fe5aSThomas Gleixner 	return 0;
11053ac7fe5aSThomas Gleixner }
11063ac7fe5aSThomas Gleixner __initcall(debug_objects_init_debugfs);
11073ac7fe5aSThomas Gleixner 
11083ac7fe5aSThomas Gleixner #else
11093ac7fe5aSThomas Gleixner static inline void debug_objects_init_debugfs(void) { }
11103ac7fe5aSThomas Gleixner #endif
11113ac7fe5aSThomas Gleixner 
11123ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
11133ac7fe5aSThomas Gleixner 
11143ac7fe5aSThomas Gleixner /* Random data structure for the self test */
11153ac7fe5aSThomas Gleixner struct self_test {
11163ac7fe5aSThomas Gleixner 	unsigned long	dummy1[6];
11173ac7fe5aSThomas Gleixner 	int		static_init;
11183ac7fe5aSThomas Gleixner 	unsigned long	dummy2[3];
11193ac7fe5aSThomas Gleixner };
11203ac7fe5aSThomas Gleixner 
1121aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test;
11223ac7fe5aSThomas Gleixner 
1123b9fdac7fSDu, Changbin static bool __init is_static_object(void *addr)
1124b9fdac7fSDu, Changbin {
1125b9fdac7fSDu, Changbin 	struct self_test *obj = addr;
1126b9fdac7fSDu, Changbin 
1127b9fdac7fSDu, Changbin 	return obj->static_init;
1128b9fdac7fSDu, Changbin }
1129b9fdac7fSDu, Changbin 
11303ac7fe5aSThomas Gleixner /*
11313ac7fe5aSThomas Gleixner  * fixup_init is called when:
11323ac7fe5aSThomas Gleixner  * - an active object is initialized
11333ac7fe5aSThomas Gleixner  */
1134b1e4d9d8SDu, Changbin static bool __init fixup_init(void *addr, enum debug_obj_state state)
11353ac7fe5aSThomas Gleixner {
11363ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11373ac7fe5aSThomas Gleixner 
11383ac7fe5aSThomas Gleixner 	switch (state) {
11393ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11403ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11413ac7fe5aSThomas Gleixner 		debug_object_init(obj, &descr_type_test);
1142b1e4d9d8SDu, Changbin 		return true;
11433ac7fe5aSThomas Gleixner 	default:
1144b1e4d9d8SDu, Changbin 		return false;
11453ac7fe5aSThomas Gleixner 	}
11463ac7fe5aSThomas Gleixner }
11473ac7fe5aSThomas Gleixner 
11483ac7fe5aSThomas Gleixner /*
11493ac7fe5aSThomas Gleixner  * fixup_activate is called when:
11503ac7fe5aSThomas Gleixner  * - an active object is activated
1151b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
11523ac7fe5aSThomas Gleixner  */
1153b1e4d9d8SDu, Changbin static bool __init fixup_activate(void *addr, enum debug_obj_state state)
11543ac7fe5aSThomas Gleixner {
11553ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11563ac7fe5aSThomas Gleixner 
11573ac7fe5aSThomas Gleixner 	switch (state) {
11583ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
1159b1e4d9d8SDu, Changbin 		return true;
11603ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11613ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11623ac7fe5aSThomas Gleixner 		debug_object_activate(obj, &descr_type_test);
1163b1e4d9d8SDu, Changbin 		return true;
11643ac7fe5aSThomas Gleixner 
11653ac7fe5aSThomas Gleixner 	default:
1166b1e4d9d8SDu, Changbin 		return false;
11673ac7fe5aSThomas Gleixner 	}
11683ac7fe5aSThomas Gleixner }
11693ac7fe5aSThomas Gleixner 
11703ac7fe5aSThomas Gleixner /*
11713ac7fe5aSThomas Gleixner  * fixup_destroy is called when:
11723ac7fe5aSThomas Gleixner  * - an active object is destroyed
11733ac7fe5aSThomas Gleixner  */
1174b1e4d9d8SDu, Changbin static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
11753ac7fe5aSThomas Gleixner {
11763ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11773ac7fe5aSThomas Gleixner 
11783ac7fe5aSThomas Gleixner 	switch (state) {
11793ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11803ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11813ac7fe5aSThomas Gleixner 		debug_object_destroy(obj, &descr_type_test);
1182b1e4d9d8SDu, Changbin 		return true;
11833ac7fe5aSThomas Gleixner 	default:
1184b1e4d9d8SDu, Changbin 		return false;
11853ac7fe5aSThomas Gleixner 	}
11863ac7fe5aSThomas Gleixner }
11873ac7fe5aSThomas Gleixner 
11883ac7fe5aSThomas Gleixner /*
11893ac7fe5aSThomas Gleixner  * fixup_free is called when:
11903ac7fe5aSThomas Gleixner  * - an active object is freed
11913ac7fe5aSThomas Gleixner  */
1192b1e4d9d8SDu, Changbin static bool __init fixup_free(void *addr, enum debug_obj_state state)
11933ac7fe5aSThomas Gleixner {
11943ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11953ac7fe5aSThomas Gleixner 
11963ac7fe5aSThomas Gleixner 	switch (state) {
11973ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11983ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11993ac7fe5aSThomas Gleixner 		debug_object_free(obj, &descr_type_test);
1200b1e4d9d8SDu, Changbin 		return true;
12013ac7fe5aSThomas Gleixner 	default:
1202b1e4d9d8SDu, Changbin 		return false;
12033ac7fe5aSThomas Gleixner 	}
12043ac7fe5aSThomas Gleixner }
12053ac7fe5aSThomas Gleixner 
12061fb2f77cSHenrik Kretzschmar static int __init
12073ac7fe5aSThomas Gleixner check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
12083ac7fe5aSThomas Gleixner {
12093ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
12103ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
12113ac7fe5aSThomas Gleixner 	unsigned long flags;
12123ac7fe5aSThomas Gleixner 	int res = -EINVAL;
12133ac7fe5aSThomas Gleixner 
12143ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
12153ac7fe5aSThomas Gleixner 
1216aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
12173ac7fe5aSThomas Gleixner 
12183ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
12193ac7fe5aSThomas Gleixner 	if (!obj && state != ODEBUG_STATE_NONE) {
12205cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
12213ac7fe5aSThomas Gleixner 		goto out;
12223ac7fe5aSThomas Gleixner 	}
12233ac7fe5aSThomas Gleixner 	if (obj && obj->state != state) {
12245cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
12253ac7fe5aSThomas Gleixner 		       obj->state, state);
12263ac7fe5aSThomas Gleixner 		goto out;
12273ac7fe5aSThomas Gleixner 	}
12283ac7fe5aSThomas Gleixner 	if (fixups != debug_objects_fixups) {
12295cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
12303ac7fe5aSThomas Gleixner 		       fixups, debug_objects_fixups);
12313ac7fe5aSThomas Gleixner 		goto out;
12323ac7fe5aSThomas Gleixner 	}
12333ac7fe5aSThomas Gleixner 	if (warnings != debug_objects_warnings) {
12345cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
12353ac7fe5aSThomas Gleixner 		       warnings, debug_objects_warnings);
12363ac7fe5aSThomas Gleixner 		goto out;
12373ac7fe5aSThomas Gleixner 	}
12383ac7fe5aSThomas Gleixner 	res = 0;
12393ac7fe5aSThomas Gleixner out:
1240aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
12413ac7fe5aSThomas Gleixner 	if (res)
1242661cc28bSThomas Gleixner 		debug_objects_enabled = false;
12433ac7fe5aSThomas Gleixner 	return res;
12443ac7fe5aSThomas Gleixner }
12453ac7fe5aSThomas Gleixner 
1246aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test = {
12473ac7fe5aSThomas Gleixner 	.name			= "selftest",
1248b9fdac7fSDu, Changbin 	.is_static_object	= is_static_object,
12493ac7fe5aSThomas Gleixner 	.fixup_init		= fixup_init,
12503ac7fe5aSThomas Gleixner 	.fixup_activate		= fixup_activate,
12513ac7fe5aSThomas Gleixner 	.fixup_destroy		= fixup_destroy,
12523ac7fe5aSThomas Gleixner 	.fixup_free		= fixup_free,
12533ac7fe5aSThomas Gleixner };
12543ac7fe5aSThomas Gleixner 
12553ac7fe5aSThomas Gleixner static __initdata struct self_test obj = { .static_init = 0 };
12563ac7fe5aSThomas Gleixner 
125755fb412eSThomas Gleixner static bool __init debug_objects_selftest(void)
12583ac7fe5aSThomas Gleixner {
12593ac7fe5aSThomas Gleixner 	int fixups, oldfixups, warnings, oldwarnings;
12603ac7fe5aSThomas Gleixner 	unsigned long flags;
12613ac7fe5aSThomas Gleixner 
12623ac7fe5aSThomas Gleixner 	local_irq_save(flags);
12633ac7fe5aSThomas Gleixner 
12643ac7fe5aSThomas Gleixner 	fixups = oldfixups = debug_objects_fixups;
12653ac7fe5aSThomas Gleixner 	warnings = oldwarnings = debug_objects_warnings;
12663ac7fe5aSThomas Gleixner 	descr_test = &descr_type_test;
12673ac7fe5aSThomas Gleixner 
12683ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12693ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
12703ac7fe5aSThomas Gleixner 		goto out;
12713ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12723ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
12733ac7fe5aSThomas Gleixner 		goto out;
12743ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12753ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
12763ac7fe5aSThomas Gleixner 		goto out;
12773ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
12783ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
12793ac7fe5aSThomas Gleixner 		goto out;
12803ac7fe5aSThomas Gleixner 	debug_object_destroy(&obj, &descr_type_test);
12813ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
12823ac7fe5aSThomas Gleixner 		goto out;
12833ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12843ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12853ac7fe5aSThomas Gleixner 		goto out;
12863ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12873ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12883ac7fe5aSThomas Gleixner 		goto out;
12893ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
12903ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12913ac7fe5aSThomas Gleixner 		goto out;
12923ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
12933ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
12943ac7fe5aSThomas Gleixner 		goto out;
12953ac7fe5aSThomas Gleixner 
12963ac7fe5aSThomas Gleixner 	obj.static_init = 1;
12973ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12989f78ff00SStephen Boyd 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
12993ac7fe5aSThomas Gleixner 		goto out;
13003ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
13013ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
13023ac7fe5aSThomas Gleixner 		goto out;
13033ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
13043ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
13053ac7fe5aSThomas Gleixner 		goto out;
13063ac7fe5aSThomas Gleixner 
13073ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
13083ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
13093ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
13103ac7fe5aSThomas Gleixner 		goto out;
13113ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13123ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
13133ac7fe5aSThomas Gleixner 		goto out;
13143ac7fe5aSThomas Gleixner 	__debug_check_no_obj_freed(&obj, sizeof(obj));
13153ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
13163ac7fe5aSThomas Gleixner 		goto out;
13173ac7fe5aSThomas Gleixner #endif
1318719e4843SFabian Frederick 	pr_info("selftest passed\n");
13193ac7fe5aSThomas Gleixner 
13203ac7fe5aSThomas Gleixner out:
13213ac7fe5aSThomas Gleixner 	debug_objects_fixups = oldfixups;
13223ac7fe5aSThomas Gleixner 	debug_objects_warnings = oldwarnings;
13233ac7fe5aSThomas Gleixner 	descr_test = NULL;
13243ac7fe5aSThomas Gleixner 
13253ac7fe5aSThomas Gleixner 	local_irq_restore(flags);
1326661cc28bSThomas Gleixner 	return debug_objects_enabled;
13273ac7fe5aSThomas Gleixner }
13283ac7fe5aSThomas Gleixner #else
132955fb412eSThomas Gleixner static inline bool debug_objects_selftest(void) { return true; }
13303ac7fe5aSThomas Gleixner #endif
13313ac7fe5aSThomas Gleixner 
13323ac7fe5aSThomas Gleixner /*
13333ac7fe5aSThomas Gleixner  * Called during early boot to initialize the hash buckets and link
13343ac7fe5aSThomas Gleixner  * the static object pool objects into the poll list. After this call
13353ac7fe5aSThomas Gleixner  * the object tracker is fully operational.
13363ac7fe5aSThomas Gleixner  */
13373ac7fe5aSThomas Gleixner void __init debug_objects_early_init(void)
13383ac7fe5aSThomas Gleixner {
13393ac7fe5aSThomas Gleixner 	int i;
13403ac7fe5aSThomas Gleixner 
13413ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1342aef9cb05SThomas Gleixner 		raw_spin_lock_init(&obj_hash[i].lock);
13433ac7fe5aSThomas Gleixner 
13443ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1345*e18328ffSThomas Gleixner 		hlist_add_head(&obj_static_pool[i].node, &pool_global.objects);
1346*e18328ffSThomas Gleixner 
1347*e18328ffSThomas Gleixner 	pool_global.cnt = ODEBUG_POOL_SIZE;
13483ac7fe5aSThomas Gleixner }
13493ac7fe5aSThomas Gleixner 
13503ac7fe5aSThomas Gleixner /*
135155fb412eSThomas Gleixner  * Convert the statically allocated objects to dynamic ones.
135255fb412eSThomas Gleixner  * debug_objects_mem_init() is called early so only one CPU is up and
135355fb412eSThomas Gleixner  * interrupts are disabled, which means it is safe to replace the active
135455fb412eSThomas Gleixner  * object references.
13551be1cb7bSThomas Gleixner  */
135655fb412eSThomas Gleixner static bool __init debug_objects_replace_static_objects(struct kmem_cache *cache)
13571be1cb7bSThomas Gleixner {
13581be1cb7bSThomas Gleixner 	struct debug_bucket *db = obj_hash;
13591be1cb7bSThomas Gleixner 	struct debug_obj *obj, *new;
136055fb412eSThomas Gleixner 	struct hlist_node *tmp;
13611be1cb7bSThomas Gleixner 	HLIST_HEAD(objects);
1362241463f4SThomas Gleixner 	int i;
13631be1cb7bSThomas Gleixner 
13641be1cb7bSThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i++) {
136555fb412eSThomas Gleixner 		obj = kmem_cache_zalloc(cache, GFP_KERNEL);
13661be1cb7bSThomas Gleixner 		if (!obj)
13671be1cb7bSThomas Gleixner 			goto free;
13681be1cb7bSThomas Gleixner 		hlist_add_head(&obj->node, &objects);
13691be1cb7bSThomas Gleixner 	}
13701be1cb7bSThomas Gleixner 
1371*e18328ffSThomas Gleixner 	debug_objects_allocated = ODEBUG_POOL_SIZE;
1372*e18328ffSThomas Gleixner 	pool_global.cnt = ODEBUG_POOL_SIZE;
1373eabb7f1aSwuchi 
13741be1cb7bSThomas Gleixner 	/*
1375a0ae9504SZhen Lei 	 * Replace the statically allocated objects list with the allocated
1376a0ae9504SZhen Lei 	 * objects list.
1377a0ae9504SZhen Lei 	 */
1378*e18328ffSThomas Gleixner 	hlist_move_list(&objects, &pool_global.objects);
13791be1cb7bSThomas Gleixner 
13801be1cb7bSThomas Gleixner 	/* Replace the active object references */
13811be1cb7bSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
13821be1cb7bSThomas Gleixner 		hlist_move_list(&db->list, &objects);
13831be1cb7bSThomas Gleixner 
1384b67bfe0dSSasha Levin 		hlist_for_each_entry(obj, &objects, node) {
1385*e18328ffSThomas Gleixner 			new = hlist_entry(pool_global.objects.first, typeof(*obj), node);
13861be1cb7bSThomas Gleixner 			hlist_del(&new->node);
1387*e18328ffSThomas Gleixner 			pool_global.cnt--;
13881be1cb7bSThomas Gleixner 			/* copy object data */
13891be1cb7bSThomas Gleixner 			*new = *obj;
13901be1cb7bSThomas Gleixner 			hlist_add_head(&new->node, &db->list);
13911be1cb7bSThomas Gleixner 		}
13921be1cb7bSThomas Gleixner 	}
139355fb412eSThomas Gleixner 	return true;
13941be1cb7bSThomas Gleixner free:
139549a5cb82SThomas Gleixner 	/* Can't use free_object_list() as the cache is not populated yet */
1396b67bfe0dSSasha Levin 	hlist_for_each_entry_safe(obj, tmp, &objects, node) {
13971be1cb7bSThomas Gleixner 		hlist_del(&obj->node);
139855fb412eSThomas Gleixner 		kmem_cache_free(cache, obj);
13991be1cb7bSThomas Gleixner 	}
140055fb412eSThomas Gleixner 	return false;
14011be1cb7bSThomas Gleixner }
14021be1cb7bSThomas Gleixner 
14031be1cb7bSThomas Gleixner /*
14043ac7fe5aSThomas Gleixner  * Called after the kmem_caches are functional to setup a dedicated
14053ac7fe5aSThomas Gleixner  * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
14063ac7fe5aSThomas Gleixner  * prevents that the debug code is called on kmem_cache_free() for the
14073ac7fe5aSThomas Gleixner  * debug tracker objects to avoid recursive calls.
14083ac7fe5aSThomas Gleixner  */
14093ac7fe5aSThomas Gleixner void __init debug_objects_mem_init(void)
14103ac7fe5aSThomas Gleixner {
141155fb412eSThomas Gleixner 	struct kmem_cache *cache;
14123f397bf9SThomas Gleixner 	int extras;
1413d86998b1SWaiman Long 
14143ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
14153ac7fe5aSThomas Gleixner 		return;
14163ac7fe5aSThomas Gleixner 
141755fb412eSThomas Gleixner 	if (!debug_objects_selftest())
1418eabb7f1aSwuchi 		return;
141955fb412eSThomas Gleixner 
142055fb412eSThomas Gleixner 	cache = kmem_cache_create("debug_objects_cache", sizeof (struct debug_obj), 0,
142155fb412eSThomas Gleixner 				  SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE, NULL);
142255fb412eSThomas Gleixner 
142355fb412eSThomas Gleixner 	if (!cache || !debug_objects_replace_static_objects(cache)) {
1424661cc28bSThomas Gleixner 		debug_objects_enabled = false;
142555fb412eSThomas Gleixner 		pr_warn("Out of memory.\n");
142655fb412eSThomas Gleixner 		return;
142755fb412eSThomas Gleixner 	}
142855fb412eSThomas Gleixner 
142955fb412eSThomas Gleixner 	/*
143055fb412eSThomas Gleixner 	 * Adjust the thresholds for allocating and freeing objects
143155fb412eSThomas Gleixner 	 * according to the number of possible CPUs available in the
143255fb412eSThomas Gleixner 	 * system.
143355fb412eSThomas Gleixner 	 */
143455fb412eSThomas Gleixner 	extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
143555fb412eSThomas Gleixner 	debug_objects_pool_size += extras;
143655fb412eSThomas Gleixner 	debug_objects_pool_min_level += extras;
143755fb412eSThomas Gleixner 
143855fb412eSThomas Gleixner 	/* Everything worked. Expose the cache */
143955fb412eSThomas Gleixner 	obj_cache = cache;
1440634d61f4SWaiman Long 
144188451f2cSZqiang #ifdef CONFIG_HOTPLUG_CPU
144288451f2cSZqiang 	cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL,
144388451f2cSZqiang 				  object_cpu_offline);
144488451f2cSZqiang #endif
144555fb412eSThomas Gleixner 	return;
14463ac7fe5aSThomas Gleixner }
1447