xref: /linux-6.15/lib/debugobjects.c (revision f57ebb92)
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 
1014077b9eSThomas Gleixner #include <linux/cpu.h>
113ac7fe5aSThomas Gleixner #include <linux/debugobjects.h>
1214077b9eSThomas Gleixner #include <linux/debugfs.h>
1314077b9eSThomas Gleixner #include <linux/hash.h>
1414077b9eSThomas Gleixner #include <linux/kmemleak.h>
15d43c36dcSAlexey Dobriyan #include <linux/sched.h>
1668db0cf1SIngo Molnar #include <linux/sched/task_stack.h>
173ac7fe5aSThomas Gleixner #include <linux/seq_file.h>
185a0e3ad6STejun Heo #include <linux/slab.h>
1914077b9eSThomas Gleixner #include <linux/static_key.h>
203ac7fe5aSThomas Gleixner 
213ac7fe5aSThomas Gleixner #define ODEBUG_HASH_BITS	14
223ac7fe5aSThomas Gleixner #define ODEBUG_HASH_SIZE	(1 << ODEBUG_HASH_BITS)
233ac7fe5aSThomas Gleixner 
2474fe1ad4SThomas Gleixner /* Must be power of two */
25634d61f4SWaiman Long #define ODEBUG_BATCH_SIZE	16
263ac7fe5aSThomas Gleixner 
2774fe1ad4SThomas Gleixner /* Initial values. Must all be a multiple of batch size */
2874fe1ad4SThomas Gleixner #define ODEBUG_POOL_SIZE	(64 * ODEBUG_BATCH_SIZE)
2974fe1ad4SThomas Gleixner #define ODEBUG_POOL_MIN_LEVEL	(ODEBUG_POOL_SIZE / 4)
3074fe1ad4SThomas Gleixner 
3174fe1ad4SThomas Gleixner #define ODEBUG_POOL_PERCPU_SIZE	(4 * ODEBUG_BATCH_SIZE)
3274fe1ad4SThomas Gleixner 
333ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_SHIFT	PAGE_SHIFT
343ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_SIZE	(1 << ODEBUG_CHUNK_SHIFT)
353ac7fe5aSThomas Gleixner #define ODEBUG_CHUNK_MASK	(~(ODEBUG_CHUNK_SIZE - 1))
363ac7fe5aSThomas Gleixner 
37a7344a68SWaiman Long /*
38a7344a68SWaiman Long  * We limit the freeing of debug objects via workqueue at a maximum
39a7344a68SWaiman Long  * frequency of 10Hz and about 1024 objects for each freeing operation.
40a7344a68SWaiman Long  * So it is freeing at most 10k debug objects per second.
41a7344a68SWaiman Long  */
429ce99c6dSThomas Gleixner #define ODEBUG_FREE_WORK_MAX	(1024 / ODEBUG_BATCH_SIZE)
43a7344a68SWaiman Long #define ODEBUG_FREE_WORK_DELAY	DIV_ROUND_UP(HZ, 10)
44a7344a68SWaiman Long 
453ac7fe5aSThomas Gleixner struct debug_bucket {
463ac7fe5aSThomas Gleixner 	struct hlist_head	list;
47aef9cb05SThomas Gleixner 	raw_spinlock_t		lock;
483ac7fe5aSThomas Gleixner };
493ac7fe5aSThomas Gleixner 
50e18328ffSThomas Gleixner struct obj_pool {
51e18328ffSThomas Gleixner 	struct hlist_head	objects;
52e18328ffSThomas Gleixner 	unsigned int		cnt;
5396a9a042SThomas Gleixner 	unsigned int		min_cnt;
5496a9a042SThomas Gleixner 	unsigned int		max_cnt;
55e18328ffSThomas Gleixner } ____cacheline_aligned;
56e18328ffSThomas Gleixner 
5796a9a042SThomas Gleixner 
5896a9a042SThomas Gleixner static DEFINE_PER_CPU_ALIGNED(struct obj_pool, pool_pcpu)  = {
5996a9a042SThomas Gleixner 	.max_cnt	= ODEBUG_POOL_PERCPU_SIZE,
6096a9a042SThomas Gleixner };
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 
6896a9a042SThomas Gleixner static struct obj_pool pool_global = {
6996a9a042SThomas Gleixner 	.min_cnt	= ODEBUG_POOL_MIN_LEVEL,
7096a9a042SThomas Gleixner 	.max_cnt	= ODEBUG_POOL_SIZE,
7196a9a042SThomas Gleixner };
7296a9a042SThomas Gleixner 
7396a9a042SThomas Gleixner static struct obj_pool pool_to_free = {
7496a9a042SThomas Gleixner 	.max_cnt	= UINT_MAX,
7596a9a042SThomas Gleixner };
763ac7fe5aSThomas Gleixner 
77cb58d190SThomas Gleixner static HLIST_HEAD(pool_boot);
78cb58d190SThomas Gleixner 
79d86998b1SWaiman Long /*
80d86998b1SWaiman Long  * Because of the presence of percpu free pools, obj_pool_free will
81d86998b1SWaiman Long  * under-count those in the percpu free pools. Similarly, obj_pool_used
82d86998b1SWaiman Long  * will over-count those in the percpu free pools. Adjustments will be
83d86998b1SWaiman Long  * made at debug_stats_show(). Both obj_pool_min_free and obj_pool_max_used
84d86998b1SWaiman Long  * can be off.
85d86998b1SWaiman Long  */
86e4757c71SZhen Lei static int __data_racy		obj_pool_min_free = ODEBUG_POOL_SIZE;
873ac7fe5aSThomas Gleixner static int			obj_pool_used;
88e4757c71SZhen Lei static int __data_racy		obj_pool_max_used;
89a7344a68SWaiman Long static bool			obj_freeing;
903ac7fe5aSThomas Gleixner 
915b5baba6SBreno Leitao static int __data_racy			debug_objects_maxchain __read_mostly;
925b5baba6SBreno Leitao static int __data_racy __maybe_unused	debug_objects_maxchecked __read_mostly;
935b5baba6SBreno Leitao static int __data_racy			debug_objects_fixups __read_mostly;
945b5baba6SBreno Leitao static int __data_racy			debug_objects_warnings __read_mostly;
95661cc28bSThomas Gleixner static bool __data_racy			debug_objects_enabled __read_mostly
963ae70205SIngo Molnar 					= CONFIG_DEBUG_OBJECTS_ENABLE_DEFAULT;
975b5baba6SBreno Leitao 
98aedcade6SStephen Boyd static const struct debug_obj_descr	*descr_test  __read_mostly;
9968279f9cSAlexey Dobriyan static struct kmem_cache		*obj_cache __ro_after_init;
1003ac7fe5aSThomas Gleixner 
101c4b73aabSWaiman Long /*
1020cad93c3SWaiman Long  * Track numbers of kmem_cache_alloc()/free() calls done.
103c4b73aabSWaiman Long  */
104e4757c71SZhen Lei static int __data_racy		debug_objects_allocated;
105e4757c71SZhen Lei static int __data_racy		debug_objects_freed;
106c4b73aabSWaiman Long 
107337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work);
108a7344a68SWaiman Long static DECLARE_DELAYED_WORK(debug_obj_work, free_obj_work);
109337fff8bSThomas Gleixner 
11014077b9eSThomas Gleixner static DEFINE_STATIC_KEY_FALSE(obj_cache_enabled);
11114077b9eSThomas Gleixner 
1123ac7fe5aSThomas Gleixner static int __init enable_object_debug(char *str)
1133ac7fe5aSThomas Gleixner {
114661cc28bSThomas Gleixner 	debug_objects_enabled = true;
1153ac7fe5aSThomas Gleixner 	return 0;
1163ac7fe5aSThomas Gleixner }
117661cc28bSThomas Gleixner early_param("debug_objects", enable_object_debug);
1183e8ebb5cSKyle McMartin 
1193e8ebb5cSKyle McMartin static int __init disable_object_debug(char *str)
1203e8ebb5cSKyle McMartin {
121661cc28bSThomas Gleixner 	debug_objects_enabled = false;
1223e8ebb5cSKyle McMartin 	return 0;
1233e8ebb5cSKyle McMartin }
1243e8ebb5cSKyle McMartin early_param("no_debug_objects", disable_object_debug);
1253ac7fe5aSThomas Gleixner 
1263ac7fe5aSThomas Gleixner static const char *obj_states[ODEBUG_STATE_MAX] = {
1273ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_NONE]		= "none",
1283ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_INIT]		= "initialized",
1293ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_INACTIVE]		= "inactive",
1303ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_ACTIVE]		= "active",
1313ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_DESTROYED]	= "destroyed",
1323ac7fe5aSThomas Gleixner 	[ODEBUG_STATE_NOTAVAILABLE]	= "not available",
1333ac7fe5aSThomas Gleixner };
1343ac7fe5aSThomas Gleixner 
135e18328ffSThomas Gleixner static __always_inline unsigned int pool_count(struct obj_pool *pool)
136e18328ffSThomas Gleixner {
137e18328ffSThomas Gleixner 	return READ_ONCE(pool->cnt);
138e18328ffSThomas Gleixner }
139e18328ffSThomas Gleixner 
14096a9a042SThomas Gleixner static __always_inline bool pool_should_refill(struct obj_pool *pool)
141e18328ffSThomas Gleixner {
14296a9a042SThomas Gleixner 	return pool_count(pool) < pool->min_cnt;
143e18328ffSThomas Gleixner }
144e18328ffSThomas Gleixner 
14596a9a042SThomas Gleixner static __always_inline bool pool_must_refill(struct obj_pool *pool)
146e18328ffSThomas Gleixner {
14796a9a042SThomas Gleixner 	return pool_count(pool) < pool->min_cnt / 2;
148e18328ffSThomas Gleixner }
149e18328ffSThomas Gleixner 
150fb60c004SThomas Gleixner static bool pool_move_batch(struct obj_pool *dst, struct obj_pool *src)
151fb60c004SThomas Gleixner {
152*f57ebb92SThomas Gleixner 	struct hlist_node *last, *next_batch, *first_batch;
153*f57ebb92SThomas Gleixner 	struct debug_obj *obj;
154*f57ebb92SThomas Gleixner 
155*f57ebb92SThomas Gleixner 	if (dst->cnt >= dst->max_cnt || !src->cnt)
156fb60c004SThomas Gleixner 		return false;
157fb60c004SThomas Gleixner 
158*f57ebb92SThomas Gleixner 	first_batch = src->objects.first;
159*f57ebb92SThomas Gleixner 	obj = hlist_entry(first_batch, typeof(*obj), node);
160*f57ebb92SThomas Gleixner 	last = obj->batch_last;
161*f57ebb92SThomas Gleixner 	next_batch = last->next;
162fb60c004SThomas Gleixner 
163*f57ebb92SThomas Gleixner 	/* Move the next batch to the front of the source pool */
164*f57ebb92SThomas Gleixner 	src->objects.first = next_batch;
165*f57ebb92SThomas Gleixner 	if (next_batch)
166*f57ebb92SThomas Gleixner 		next_batch->pprev = &src->objects.first;
167fb60c004SThomas Gleixner 
168*f57ebb92SThomas Gleixner 	/* Add the extracted batch to the destination pool */
169*f57ebb92SThomas Gleixner 	last->next = dst->objects.first;
170*f57ebb92SThomas Gleixner 	if (last->next)
171*f57ebb92SThomas Gleixner 		last->next->pprev = &last->next;
172*f57ebb92SThomas Gleixner 	first_batch->pprev = &dst->objects.first;
173*f57ebb92SThomas Gleixner 	dst->objects.first = first_batch;
174*f57ebb92SThomas Gleixner 
175*f57ebb92SThomas Gleixner 	WRITE_ONCE(src->cnt, src->cnt - ODEBUG_BATCH_SIZE);
176*f57ebb92SThomas Gleixner 	WRITE_ONCE(dst->cnt, dst->cnt + ODEBUG_BATCH_SIZE);
177fb60c004SThomas Gleixner 	return true;
178fb60c004SThomas Gleixner }
179fb60c004SThomas Gleixner 
180aebbfe07SThomas Gleixner static bool pool_push_batch(struct obj_pool *dst, struct hlist_head *head)
181aebbfe07SThomas Gleixner {
182aebbfe07SThomas Gleixner 	struct hlist_node *last;
183aebbfe07SThomas Gleixner 	struct debug_obj *obj;
184aebbfe07SThomas Gleixner 
185aebbfe07SThomas Gleixner 	if (dst->cnt >= dst->max_cnt)
186aebbfe07SThomas Gleixner 		return false;
187aebbfe07SThomas Gleixner 
188aebbfe07SThomas Gleixner 	obj = hlist_entry(head->first, typeof(*obj), node);
189aebbfe07SThomas Gleixner 	last = obj->batch_last;
190aebbfe07SThomas Gleixner 
191aebbfe07SThomas Gleixner 	hlist_splice_init(head, last, &dst->objects);
192aebbfe07SThomas Gleixner 	WRITE_ONCE(dst->cnt, dst->cnt + ODEBUG_BATCH_SIZE);
193aebbfe07SThomas Gleixner 	return true;
194aebbfe07SThomas Gleixner }
195aebbfe07SThomas Gleixner 
1969ce99c6dSThomas Gleixner static bool pool_pop_batch(struct hlist_head *head, struct obj_pool *src)
1979ce99c6dSThomas Gleixner {
198*f57ebb92SThomas Gleixner 	struct hlist_node *last, *next;
199*f57ebb92SThomas Gleixner 	struct debug_obj *obj;
200*f57ebb92SThomas Gleixner 
2019ce99c6dSThomas Gleixner 	if (!src->cnt)
2029ce99c6dSThomas Gleixner 		return false;
2039ce99c6dSThomas Gleixner 
204*f57ebb92SThomas Gleixner 	/* Move the complete list to the head */
205*f57ebb92SThomas Gleixner 	hlist_move_list(&src->objects, head);
2069ce99c6dSThomas Gleixner 
207*f57ebb92SThomas Gleixner 	obj = hlist_entry(head->first, typeof(*obj), node);
208*f57ebb92SThomas Gleixner 	last = obj->batch_last;
209*f57ebb92SThomas Gleixner 	next = last->next;
210*f57ebb92SThomas Gleixner 	/* Disconnect the batch from the list */
211*f57ebb92SThomas Gleixner 	last->next = NULL;
212*f57ebb92SThomas Gleixner 
213*f57ebb92SThomas Gleixner 	/* Move the node after last back to the source pool. */
214*f57ebb92SThomas Gleixner 	src->objects.first = next;
215*f57ebb92SThomas Gleixner 	if (next)
216*f57ebb92SThomas Gleixner 		next->pprev = &src->objects.first;
217*f57ebb92SThomas Gleixner 
218*f57ebb92SThomas Gleixner 	WRITE_ONCE(src->cnt, src->cnt - ODEBUG_BATCH_SIZE);
2199ce99c6dSThomas Gleixner 	return true;
2209ce99c6dSThomas Gleixner }
2219ce99c6dSThomas Gleixner 
222fb60c004SThomas Gleixner static struct debug_obj *__alloc_object(struct hlist_head *list)
223fb60c004SThomas Gleixner {
224fb60c004SThomas Gleixner 	struct debug_obj *obj;
225fb60c004SThomas Gleixner 
226fb60c004SThomas Gleixner 	if (unlikely(!list->first))
227fb60c004SThomas Gleixner 		return NULL;
228fb60c004SThomas Gleixner 
229fb60c004SThomas Gleixner 	obj = hlist_entry(list->first, typeof(*obj), node);
230fb60c004SThomas Gleixner 	hlist_del(&obj->node);
231fb60c004SThomas Gleixner 	return obj;
232fb60c004SThomas Gleixner }
233fb60c004SThomas Gleixner 
234fb60c004SThomas Gleixner static struct debug_obj *pcpu_alloc(void)
235fb60c004SThomas Gleixner {
236fb60c004SThomas Gleixner 	struct obj_pool *pcp = this_cpu_ptr(&pool_pcpu);
237fb60c004SThomas Gleixner 
238fb60c004SThomas Gleixner 	lockdep_assert_irqs_disabled();
239fb60c004SThomas Gleixner 
240fb60c004SThomas Gleixner 	for (;;) {
241fb60c004SThomas Gleixner 		struct debug_obj *obj = __alloc_object(&pcp->objects);
242fb60c004SThomas Gleixner 
243fb60c004SThomas Gleixner 		if (likely(obj)) {
244fb60c004SThomas Gleixner 			pcp->cnt--;
245fb60c004SThomas Gleixner 			return obj;
246fb60c004SThomas Gleixner 		}
247fb60c004SThomas Gleixner 
248fb60c004SThomas Gleixner 		guard(raw_spinlock)(&pool_lock);
249fb60c004SThomas Gleixner 		if (!pool_move_batch(pcp, &pool_to_free)) {
250fb60c004SThomas Gleixner 			if (!pool_move_batch(pcp, &pool_global))
251fb60c004SThomas Gleixner 				return NULL;
252fb60c004SThomas Gleixner 		}
253*f57ebb92SThomas Gleixner 		obj_pool_used += ODEBUG_BATCH_SIZE;
254fb60c004SThomas Gleixner 
255fb60c004SThomas Gleixner 		if (obj_pool_used > obj_pool_max_used)
256fb60c004SThomas Gleixner 			obj_pool_max_used = obj_pool_used;
257fb60c004SThomas Gleixner 
258fb60c004SThomas Gleixner 		if (pool_global.cnt < obj_pool_min_free)
259fb60c004SThomas Gleixner 			obj_pool_min_free = pool_global.cnt;
260fb60c004SThomas Gleixner 	}
261fb60c004SThomas Gleixner }
262fb60c004SThomas Gleixner 
263a3b9e191SThomas Gleixner static void pcpu_free(struct debug_obj *obj)
264a3b9e191SThomas Gleixner {
265a3b9e191SThomas Gleixner 	struct obj_pool *pcp = this_cpu_ptr(&pool_pcpu);
266*f57ebb92SThomas Gleixner 	struct debug_obj *first;
267a3b9e191SThomas Gleixner 
268a3b9e191SThomas Gleixner 	lockdep_assert_irqs_disabled();
269a3b9e191SThomas Gleixner 
270*f57ebb92SThomas Gleixner 	if (!(pcp->cnt % ODEBUG_BATCH_SIZE)) {
271*f57ebb92SThomas Gleixner 		obj->batch_last = &obj->node;
272*f57ebb92SThomas Gleixner 	} else {
273*f57ebb92SThomas Gleixner 		first = hlist_entry(pcp->objects.first, typeof(*first), node);
274*f57ebb92SThomas Gleixner 		obj->batch_last = first->batch_last;
275*f57ebb92SThomas Gleixner 	}
276a3b9e191SThomas Gleixner 	hlist_add_head(&obj->node, &pcp->objects);
277a3b9e191SThomas Gleixner 	pcp->cnt++;
278a3b9e191SThomas Gleixner 
279a3b9e191SThomas Gleixner 	/* Pool full ? */
280a3b9e191SThomas Gleixner 	if (pcp->cnt < ODEBUG_POOL_PERCPU_SIZE)
281a3b9e191SThomas Gleixner 		return;
282a3b9e191SThomas Gleixner 
283a3b9e191SThomas Gleixner 	/* Remove a batch from the per CPU pool */
284a3b9e191SThomas Gleixner 	guard(raw_spinlock)(&pool_lock);
285a3b9e191SThomas Gleixner 	/* Try to fit the batch into the pool_global first */
286a3b9e191SThomas Gleixner 	if (!pool_move_batch(&pool_global, pcp))
287a3b9e191SThomas Gleixner 		pool_move_batch(&pool_to_free, pcp);
288a3b9e191SThomas Gleixner 	obj_pool_used -= ODEBUG_BATCH_SIZE;
289a3b9e191SThomas Gleixner }
290a3b9e191SThomas Gleixner 
29149a5cb82SThomas Gleixner static void free_object_list(struct hlist_head *head)
29249a5cb82SThomas Gleixner {
29349a5cb82SThomas Gleixner 	struct hlist_node *tmp;
29449a5cb82SThomas Gleixner 	struct debug_obj *obj;
29549a5cb82SThomas Gleixner 	int cnt = 0;
29649a5cb82SThomas Gleixner 
29749a5cb82SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, head, node) {
29849a5cb82SThomas Gleixner 		hlist_del(&obj->node);
29949a5cb82SThomas Gleixner 		kmem_cache_free(obj_cache, obj);
30049a5cb82SThomas Gleixner 		cnt++;
30149a5cb82SThomas Gleixner 	}
30249a5cb82SThomas Gleixner 	debug_objects_freed += cnt;
30349a5cb82SThomas Gleixner }
30449a5cb82SThomas Gleixner 
305d8c6cd3aSZhen Lei static void fill_pool_from_freelist(void)
3063ac7fe5aSThomas Gleixner {
307d8c6cd3aSZhen Lei 	static unsigned long state;
3083ac7fe5aSThomas Gleixner 
30936c4ead6SYang Shi 	/*
31063a4a9b5SZhen Lei 	 * Reuse objs from the global obj_to_free list; they will be
31163a4a9b5SZhen Lei 	 * reinitialized when allocating.
31236c4ead6SYang Shi 	 */
313e18328ffSThomas Gleixner 	if (!pool_count(&pool_to_free))
314d8c6cd3aSZhen Lei 		return;
315d8c6cd3aSZhen Lei 
316d8c6cd3aSZhen Lei 	/*
317d8c6cd3aSZhen Lei 	 * Prevent the context from being scheduled or interrupted after
318d8c6cd3aSZhen Lei 	 * setting the state flag;
319d8c6cd3aSZhen Lei 	 */
320d8c6cd3aSZhen Lei 	guard(irqsave)();
321d8c6cd3aSZhen Lei 
322d8c6cd3aSZhen Lei 	/*
323d8c6cd3aSZhen Lei 	 * Avoid lock contention on &pool_lock and avoid making the cache
324d8c6cd3aSZhen Lei 	 * line exclusive by testing the bit before attempting to set it.
325d8c6cd3aSZhen Lei 	 */
326d8c6cd3aSZhen Lei 	if (test_bit(0, &state) || test_and_set_bit(0, &state))
327d8c6cd3aSZhen Lei 		return;
328d8c6cd3aSZhen Lei 
329fb60c004SThomas Gleixner 	/* Avoid taking the lock when there is no work to do */
330fb60c004SThomas Gleixner 	while (pool_should_refill(&pool_global) && pool_count(&pool_to_free)) {
331d8c6cd3aSZhen Lei 		guard(raw_spinlock)(&pool_lock);
332fb60c004SThomas Gleixner 		/* Move a batch if possible */
333fb60c004SThomas Gleixner 		pool_move_batch(&pool_global, &pool_to_free);
33436c4ead6SYang Shi 	}
335d8c6cd3aSZhen Lei 	clear_bit(0, &state);
33636c4ead6SYang Shi }
33736c4ead6SYang Shi 
338aebbfe07SThomas Gleixner static bool kmem_alloc_batch(struct hlist_head *head, struct kmem_cache *cache, gfp_t gfp)
339aebbfe07SThomas Gleixner {
340aebbfe07SThomas Gleixner 	struct hlist_node *last = NULL;
341aebbfe07SThomas Gleixner 	struct debug_obj *obj;
342aebbfe07SThomas Gleixner 
343aebbfe07SThomas Gleixner 	for (int cnt = 0; cnt < ODEBUG_BATCH_SIZE; cnt++) {
344aebbfe07SThomas Gleixner 		obj = kmem_cache_zalloc(cache, gfp);
345aebbfe07SThomas Gleixner 		if (!obj) {
346aebbfe07SThomas Gleixner 			free_object_list(head);
347aebbfe07SThomas Gleixner 			return false;
348aebbfe07SThomas Gleixner 		}
349aebbfe07SThomas Gleixner 		debug_objects_allocated++;
350aebbfe07SThomas Gleixner 
351aebbfe07SThomas Gleixner 		if (!last)
352aebbfe07SThomas Gleixner 			last = &obj->node;
353aebbfe07SThomas Gleixner 		obj->batch_last = last;
354aebbfe07SThomas Gleixner 
355aebbfe07SThomas Gleixner 		hlist_add_head(&obj->node, head);
356aebbfe07SThomas Gleixner 	}
357aebbfe07SThomas Gleixner 	return true;
358aebbfe07SThomas Gleixner }
359aebbfe07SThomas Gleixner 
360d8c6cd3aSZhen Lei static void fill_pool(void)
361d8c6cd3aSZhen Lei {
362d8c6cd3aSZhen Lei 	static atomic_t cpus_allocating;
363d8c6cd3aSZhen Lei 
364d8c6cd3aSZhen Lei 	/*
365d8c6cd3aSZhen Lei 	 * Avoid allocation and lock contention when:
366d8c6cd3aSZhen Lei 	 *   - One other CPU is already allocating
367d8c6cd3aSZhen Lei 	 *   - the global pool has not reached the critical level yet
368d8c6cd3aSZhen Lei 	 */
36996a9a042SThomas Gleixner 	if (!pool_must_refill(&pool_global) && atomic_read(&cpus_allocating))
3701fda107dSThomas Gleixner 		return;
3713ac7fe5aSThomas Gleixner 
372d8c6cd3aSZhen Lei 	atomic_inc(&cpus_allocating);
37396a9a042SThomas Gleixner 	while (pool_should_refill(&pool_global)) {
374813fd078SZhen Lei 		HLIST_HEAD(head);
3753ac7fe5aSThomas Gleixner 
376aebbfe07SThomas Gleixner 		if (!kmem_alloc_batch(&head, obj_cache, __GFP_HIGH | __GFP_NOWARN))
377d8c6cd3aSZhen Lei 			break;
3783ac7fe5aSThomas Gleixner 
379d8c6cd3aSZhen Lei 		guard(raw_spinlock_irqsave)(&pool_lock);
380aebbfe07SThomas Gleixner 		if (!pool_push_batch(&pool_global, &head))
381aebbfe07SThomas Gleixner 			pool_push_batch(&pool_to_free, &head);
3823ac7fe5aSThomas Gleixner 	}
383d8c6cd3aSZhen Lei 	atomic_dec(&cpus_allocating);
3843ac7fe5aSThomas Gleixner }
3853ac7fe5aSThomas Gleixner 
3863ac7fe5aSThomas Gleixner /*
3873ac7fe5aSThomas Gleixner  * Lookup an object in the hash bucket.
3883ac7fe5aSThomas Gleixner  */
3893ac7fe5aSThomas Gleixner static struct debug_obj *lookup_object(void *addr, struct debug_bucket *b)
3903ac7fe5aSThomas Gleixner {
3913ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
3923ac7fe5aSThomas Gleixner 	int cnt = 0;
3933ac7fe5aSThomas Gleixner 
394b67bfe0dSSasha Levin 	hlist_for_each_entry(obj, &b->list, node) {
3953ac7fe5aSThomas Gleixner 		cnt++;
3963ac7fe5aSThomas Gleixner 		if (obj->object == addr)
3973ac7fe5aSThomas Gleixner 			return obj;
3983ac7fe5aSThomas Gleixner 	}
3993ac7fe5aSThomas Gleixner 	if (cnt > debug_objects_maxchain)
4003ac7fe5aSThomas Gleixner 		debug_objects_maxchain = cnt;
4013ac7fe5aSThomas Gleixner 
4023ac7fe5aSThomas Gleixner 	return NULL;
4033ac7fe5aSThomas Gleixner }
4043ac7fe5aSThomas Gleixner 
405fb60c004SThomas Gleixner static struct debug_obj *alloc_object(void *addr, struct debug_bucket *b,
406fb60c004SThomas Gleixner 				      const struct debug_obj_descr *descr)
407d86998b1SWaiman Long {
408d86998b1SWaiman Long 	struct debug_obj *obj;
409d86998b1SWaiman Long 
41014077b9eSThomas Gleixner 	if (static_branch_likely(&obj_cache_enabled))
411fb60c004SThomas Gleixner 		obj = pcpu_alloc();
412fb60c004SThomas Gleixner 	else
413cb58d190SThomas Gleixner 		obj = __alloc_object(&pool_boot);
4143ac7fe5aSThomas Gleixner 
415fb60c004SThomas Gleixner 	if (likely(obj)) {
416d86998b1SWaiman Long 		obj->object = addr;
417d86998b1SWaiman Long 		obj->descr  = descr;
418d86998b1SWaiman Long 		obj->state  = ODEBUG_STATE_NONE;
419d86998b1SWaiman Long 		obj->astate = 0;
420d86998b1SWaiman Long 		hlist_add_head(&obj->node, &b->list);
421d86998b1SWaiman Long 	}
4223ac7fe5aSThomas Gleixner 	return obj;
4233ac7fe5aSThomas Gleixner }
4243ac7fe5aSThomas Gleixner 
4259ce99c6dSThomas Gleixner /* workqueue function to free objects. */
426337fff8bSThomas Gleixner static void free_obj_work(struct work_struct *work)
427337fff8bSThomas Gleixner {
4289ce99c6dSThomas Gleixner 	bool free = true;
429337fff8bSThomas Gleixner 
430a7344a68SWaiman Long 	WRITE_ONCE(obj_freeing, false);
4319ce99c6dSThomas Gleixner 
4329ce99c6dSThomas Gleixner 	if (!pool_count(&pool_to_free))
433858274b6SWaiman Long 		return;
43436c4ead6SYang Shi 
4359ce99c6dSThomas Gleixner 	for (unsigned int cnt = 0; cnt < ODEBUG_FREE_WORK_MAX; cnt++) {
4369ce99c6dSThomas Gleixner 		HLIST_HEAD(tofree);
437a7344a68SWaiman Long 
4389ce99c6dSThomas Gleixner 		/* Acquire and drop the lock for each batch */
4399ce99c6dSThomas Gleixner 		scoped_guard(raw_spinlock_irqsave, &pool_lock) {
4409ce99c6dSThomas Gleixner 			if (!pool_to_free.cnt)
441a7344a68SWaiman Long 				return;
44236c4ead6SYang Shi 
4439ce99c6dSThomas Gleixner 			/* Refill the global pool if possible */
4449ce99c6dSThomas Gleixner 			if (pool_move_batch(&pool_global, &pool_to_free)) {
4459ce99c6dSThomas Gleixner 				/* Don't free as there seems to be demand */
4469ce99c6dSThomas Gleixner 				free = false;
4479ce99c6dSThomas Gleixner 			} else if (free) {
4489ce99c6dSThomas Gleixner 				pool_pop_batch(&tofree, &pool_to_free);
4499ce99c6dSThomas Gleixner 			} else {
4509ce99c6dSThomas Gleixner 				return;
45136c4ead6SYang Shi 			}
4529ce99c6dSThomas Gleixner 		}
45349a5cb82SThomas Gleixner 		free_object_list(&tofree);
454337fff8bSThomas Gleixner 	}
4559ce99c6dSThomas Gleixner }
456337fff8bSThomas Gleixner 
457a7344a68SWaiman Long static void __free_object(struct debug_obj *obj)
458636e1970SYang Shi {
459cb58d190SThomas Gleixner 	guard(irqsave)();
46014077b9eSThomas Gleixner 	if (static_branch_likely(&obj_cache_enabled))
461a3b9e191SThomas Gleixner 		pcpu_free(obj);
462a3b9e191SThomas Gleixner 	else
463cb58d190SThomas Gleixner 		hlist_add_head(&obj->node, &pool_boot);
464636e1970SYang Shi }
465636e1970SYang Shi 
466337fff8bSThomas Gleixner /*
467337fff8bSThomas Gleixner  * Put the object back into the pool and schedule work to free objects
468337fff8bSThomas Gleixner  * if necessary.
4693ac7fe5aSThomas Gleixner  */
4703ac7fe5aSThomas Gleixner static void free_object(struct debug_obj *obj)
4713ac7fe5aSThomas Gleixner {
472a7344a68SWaiman Long 	__free_object(obj);
473e18328ffSThomas Gleixner 	if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
474a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
475a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
476a7344a68SWaiman Long 	}
4773ac7fe5aSThomas Gleixner }
4783ac7fe5aSThomas Gleixner 
479a2a70238SThomas Gleixner static void put_objects(struct hlist_head *list)
48088451f2cSZqiang {
48188451f2cSZqiang 	struct hlist_node *tmp;
48288451f2cSZqiang 	struct debug_obj *obj;
48388451f2cSZqiang 
484a2a70238SThomas Gleixner 	/*
485a2a70238SThomas Gleixner 	 * Using free_object() puts the objects into reuse or schedules
486a2a70238SThomas Gleixner 	 * them for freeing and it get's all the accounting correct.
487a2a70238SThomas Gleixner 	 */
488a2a70238SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, list, node) {
48988451f2cSZqiang 		hlist_del(&obj->node);
490a2a70238SThomas Gleixner 		free_object(obj);
491a2a70238SThomas Gleixner 	}
49288451f2cSZqiang }
493eabb7f1aSwuchi 
49449968cf1SThomas Gleixner #ifdef CONFIG_HOTPLUG_CPU
495a2a70238SThomas Gleixner static int object_cpu_offline(unsigned int cpu)
496a2a70238SThomas Gleixner {
497a2a70238SThomas Gleixner 	/* Remote access is safe as the CPU is dead already */
49818b8afcbSThomas Gleixner 	struct obj_pool *pcp = per_cpu_ptr(&pool_pcpu, cpu);
499eabb7f1aSwuchi 
50018b8afcbSThomas Gleixner 	put_objects(&pcp->objects);
50118b8afcbSThomas Gleixner 	pcp->cnt = 0;
50288451f2cSZqiang 	return 0;
50388451f2cSZqiang }
50488451f2cSZqiang #endif
50588451f2cSZqiang 
50649968cf1SThomas Gleixner /* Out of memory. Free all objects from hash */
5073ac7fe5aSThomas Gleixner static void debug_objects_oom(void)
5083ac7fe5aSThomas Gleixner {
5093ac7fe5aSThomas Gleixner 	struct debug_bucket *db = obj_hash;
510673d62ccSVegard Nossum 	HLIST_HEAD(freelist);
5113ac7fe5aSThomas Gleixner 
512719e4843SFabian Frederick 	pr_warn("Out of memory. ODEBUG disabled\n");
5133ac7fe5aSThomas Gleixner 
51449968cf1SThomas Gleixner 	for (int i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
51549968cf1SThomas Gleixner 		scoped_guard(raw_spinlock_irqsave, &db->lock)
516673d62ccSVegard Nossum 			hlist_move_list(&db->list, &freelist);
517673d62ccSVegard Nossum 
51849968cf1SThomas Gleixner 		put_objects(&freelist);
5193ac7fe5aSThomas Gleixner 	}
5203ac7fe5aSThomas Gleixner }
5213ac7fe5aSThomas Gleixner 
5223ac7fe5aSThomas Gleixner /*
5233ac7fe5aSThomas Gleixner  * We use the pfn of the address for the hash. That way we can check
5243ac7fe5aSThomas Gleixner  * for freed objects simply by checking the affected bucket.
5253ac7fe5aSThomas Gleixner  */
5263ac7fe5aSThomas Gleixner static struct debug_bucket *get_bucket(unsigned long addr)
5273ac7fe5aSThomas Gleixner {
5283ac7fe5aSThomas Gleixner 	unsigned long hash;
5293ac7fe5aSThomas Gleixner 
5303ac7fe5aSThomas Gleixner 	hash = hash_long((addr >> ODEBUG_CHUNK_SHIFT), ODEBUG_HASH_BITS);
5313ac7fe5aSThomas Gleixner 	return &obj_hash[hash];
5323ac7fe5aSThomas Gleixner }
5333ac7fe5aSThomas Gleixner 
5343ac7fe5aSThomas Gleixner static void debug_print_object(struct debug_obj *obj, char *msg)
5353ac7fe5aSThomas Gleixner {
536aedcade6SStephen Boyd 	const struct debug_obj_descr *descr = obj->descr;
5373ac7fe5aSThomas Gleixner 	static int limit;
5383ac7fe5aSThomas Gleixner 
5398b64d420STetsuo Handa 	/*
5408b64d420STetsuo Handa 	 * Don't report if lookup_object_or_alloc() by the current thread
5418b64d420STetsuo Handa 	 * failed because lookup_object_or_alloc()/debug_objects_oom() by a
5428b64d420STetsuo Handa 	 * concurrent thread turned off debug_objects_enabled and cleared
5438b64d420STetsuo Handa 	 * the hash buckets.
5448b64d420STetsuo Handa 	 */
5458b64d420STetsuo Handa 	if (!debug_objects_enabled)
5468b64d420STetsuo Handa 		return;
5478b64d420STetsuo Handa 
54899777288SStanislaw Gruszka 	if (limit < 5 && descr != descr_test) {
54999777288SStanislaw Gruszka 		void *hint = descr->debug_hint ?
55099777288SStanislaw Gruszka 			descr->debug_hint(obj->object) : NULL;
5513ac7fe5aSThomas Gleixner 		limit++;
552a5d8e467SMathieu Desnoyers 		WARN(1, KERN_ERR "ODEBUG: %s %s (active state %u) "
553c4db2d3bSStephen Boyd 				 "object: %p object type: %s hint: %pS\n",
554a5d8e467SMathieu Desnoyers 			msg, obj_states[obj->state], obj->astate,
555c4db2d3bSStephen Boyd 			obj->object, descr->name, hint);
5563ac7fe5aSThomas Gleixner 	}
5573ac7fe5aSThomas Gleixner 	debug_objects_warnings++;
5583ac7fe5aSThomas Gleixner }
5593ac7fe5aSThomas Gleixner 
5603ac7fe5aSThomas Gleixner /*
5613ac7fe5aSThomas Gleixner  * Try to repair the damage, so we have a better chance to get useful
5623ac7fe5aSThomas Gleixner  * debug output.
5633ac7fe5aSThomas Gleixner  */
564b1e4d9d8SDu, Changbin static bool
565b1e4d9d8SDu, Changbin debug_object_fixup(bool (*fixup)(void *addr, enum debug_obj_state state),
5663ac7fe5aSThomas Gleixner 		   void * addr, enum debug_obj_state state)
5673ac7fe5aSThomas Gleixner {
568b1e4d9d8SDu, Changbin 	if (fixup && fixup(addr, state)) {
569b1e4d9d8SDu, Changbin 		debug_objects_fixups++;
570b1e4d9d8SDu, Changbin 		return true;
571b1e4d9d8SDu, Changbin 	}
572b1e4d9d8SDu, Changbin 	return false;
5733ac7fe5aSThomas Gleixner }
5743ac7fe5aSThomas Gleixner 
5753ac7fe5aSThomas Gleixner static void debug_object_is_on_stack(void *addr, int onstack)
5763ac7fe5aSThomas Gleixner {
5773ac7fe5aSThomas Gleixner 	int is_on_stack;
5783ac7fe5aSThomas Gleixner 	static int limit;
5793ac7fe5aSThomas Gleixner 
5803ac7fe5aSThomas Gleixner 	if (limit > 4)
5813ac7fe5aSThomas Gleixner 		return;
5823ac7fe5aSThomas Gleixner 
5838b05c7e6SFUJITA Tomonori 	is_on_stack = object_is_on_stack(addr);
5843ac7fe5aSThomas Gleixner 	if (is_on_stack == onstack)
5853ac7fe5aSThomas Gleixner 		return;
5863ac7fe5aSThomas Gleixner 
5873ac7fe5aSThomas Gleixner 	limit++;
5883ac7fe5aSThomas Gleixner 	if (is_on_stack)
589fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is on stack %p, but NOT annotated.\n", addr,
590fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
5913ac7fe5aSThomas Gleixner 	else
592fc91a3c4SJoel Fernandes (Google) 		pr_warn("object %p is NOT on stack %p, but annotated.\n", addr,
593fc91a3c4SJoel Fernandes (Google) 			 task_stack_page(current));
594fc91a3c4SJoel Fernandes (Google) 
5953ac7fe5aSThomas Gleixner 	WARN_ON(1);
5963ac7fe5aSThomas Gleixner }
5973ac7fe5aSThomas Gleixner 
59863a75969SThomas Gleixner static struct debug_obj *lookup_object_or_alloc(void *addr, struct debug_bucket *b,
59963a75969SThomas Gleixner 						const struct debug_obj_descr *descr,
60063a75969SThomas Gleixner 						bool onstack, bool alloc_ifstatic)
60163a75969SThomas Gleixner {
60263a75969SThomas Gleixner 	struct debug_obj *obj = lookup_object(addr, b);
60363a75969SThomas Gleixner 	enum debug_obj_state state = ODEBUG_STATE_NONE;
60463a75969SThomas Gleixner 
60563a75969SThomas Gleixner 	if (likely(obj))
60663a75969SThomas Gleixner 		return obj;
60763a75969SThomas Gleixner 
60863a75969SThomas Gleixner 	/*
60963a75969SThomas Gleixner 	 * debug_object_init() unconditionally allocates untracked
61063a75969SThomas Gleixner 	 * objects. It does not matter whether it is a static object or
61163a75969SThomas Gleixner 	 * not.
61263a75969SThomas Gleixner 	 *
61363a75969SThomas Gleixner 	 * debug_object_assert_init() and debug_object_activate() allow
61463a75969SThomas Gleixner 	 * allocation only if the descriptor callback confirms that the
61563a75969SThomas Gleixner 	 * object is static and considered initialized. For non-static
61663a75969SThomas Gleixner 	 * objects the allocation needs to be done from the fixup callback.
61763a75969SThomas Gleixner 	 */
61863a75969SThomas Gleixner 	if (unlikely(alloc_ifstatic)) {
61963a75969SThomas Gleixner 		if (!descr->is_static_object || !descr->is_static_object(addr))
62063a75969SThomas Gleixner 			return ERR_PTR(-ENOENT);
62163a75969SThomas Gleixner 		/* Statically allocated objects are considered initialized */
62263a75969SThomas Gleixner 		state = ODEBUG_STATE_INIT;
62363a75969SThomas Gleixner 	}
62463a75969SThomas Gleixner 
62563a75969SThomas Gleixner 	obj = alloc_object(addr, b, descr);
62663a75969SThomas Gleixner 	if (likely(obj)) {
62763a75969SThomas Gleixner 		obj->state = state;
62863a75969SThomas Gleixner 		debug_object_is_on_stack(addr, onstack);
62963a75969SThomas Gleixner 		return obj;
63063a75969SThomas Gleixner 	}
63163a75969SThomas Gleixner 
63263a75969SThomas Gleixner 	/* Out of memory. Do the cleanup outside of the locked region */
633661cc28bSThomas Gleixner 	debug_objects_enabled = false;
63463a75969SThomas Gleixner 	return NULL;
63563a75969SThomas Gleixner }
63663a75969SThomas Gleixner 
6370af462f1SThomas Gleixner static void debug_objects_fill_pool(void)
6380af462f1SThomas Gleixner {
63914077b9eSThomas Gleixner 	if (!static_branch_likely(&obj_cache_enabled))
640d8c6cd3aSZhen Lei 		return;
641d8c6cd3aSZhen Lei 
64296a9a042SThomas Gleixner 	if (likely(!pool_should_refill(&pool_global)))
643d8c6cd3aSZhen Lei 		return;
644d8c6cd3aSZhen Lei 
645d8c6cd3aSZhen Lei 	/* Try reusing objects from obj_to_free_list */
646d8c6cd3aSZhen Lei 	fill_pool_from_freelist();
647d8c6cd3aSZhen Lei 
64896a9a042SThomas Gleixner 	if (likely(!pool_should_refill(&pool_global)))
649d8c6cd3aSZhen Lei 		return;
650d8c6cd3aSZhen Lei 
6510af462f1SThomas Gleixner 	/*
6520af462f1SThomas Gleixner 	 * On RT enabled kernels the pool refill must happen in preemptible
6530cce06baSPeter Zijlstra 	 * context -- for !RT kernels we rely on the fact that spinlock_t and
6540cce06baSPeter Zijlstra 	 * raw_spinlock_t are basically the same type and this lock-type
6550cce06baSPeter Zijlstra 	 * inversion works just fine.
6560af462f1SThomas Gleixner 	 */
6570cce06baSPeter Zijlstra 	if (!IS_ENABLED(CONFIG_PREEMPT_RT) || preemptible()) {
6580cce06baSPeter Zijlstra 		/*
6590cce06baSPeter Zijlstra 		 * Annotate away the spinlock_t inside raw_spinlock_t warning
6600cce06baSPeter Zijlstra 		 * by temporarily raising the wait-type to WAIT_SLEEP, matching
6610cce06baSPeter Zijlstra 		 * the preemptible() condition above.
6620cce06baSPeter Zijlstra 		 */
6630cce06baSPeter Zijlstra 		static DEFINE_WAIT_OVERRIDE_MAP(fill_pool_map, LD_WAIT_SLEEP);
6640cce06baSPeter Zijlstra 		lock_map_acquire_try(&fill_pool_map);
6650af462f1SThomas Gleixner 		fill_pool();
6660cce06baSPeter Zijlstra 		lock_map_release(&fill_pool_map);
6670cce06baSPeter Zijlstra 	}
6680af462f1SThomas Gleixner }
6690af462f1SThomas Gleixner 
6703ac7fe5aSThomas Gleixner static void
671aedcade6SStephen Boyd __debug_object_init(void *addr, const struct debug_obj_descr *descr, int onstack)
6723ac7fe5aSThomas Gleixner {
6739bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
6743ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
6753ac7fe5aSThomas Gleixner 	unsigned long flags;
6763ac7fe5aSThomas Gleixner 
6770af462f1SThomas Gleixner 	debug_objects_fill_pool();
67850db04ddSVegard Nossum 
6793ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
6803ac7fe5aSThomas Gleixner 
681aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
6823ac7fe5aSThomas Gleixner 
68363a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, onstack, false);
68463a75969SThomas Gleixner 	if (unlikely(!obj)) {
685aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
6863ac7fe5aSThomas Gleixner 		debug_objects_oom();
6873ac7fe5aSThomas Gleixner 		return;
6883ac7fe5aSThomas Gleixner 	}
6893ac7fe5aSThomas Gleixner 
6903ac7fe5aSThomas Gleixner 	switch (obj->state) {
6913ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
6923ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
6933ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
6943ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_INIT;
695aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
696d5f34153SWaiman Long 		return;
6973ac7fe5aSThomas Gleixner 	default:
6983ac7fe5aSThomas Gleixner 		break;
6993ac7fe5aSThomas Gleixner 	}
7003ac7fe5aSThomas Gleixner 
7019bb63626SAndrzej Hajda 	o = *obj;
702aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
7039bb63626SAndrzej Hajda 	debug_print_object(&o, "init");
7049bb63626SAndrzej Hajda 
7059bb63626SAndrzej Hajda 	if (o.state == ODEBUG_STATE_ACTIVE)
7069bb63626SAndrzej Hajda 		debug_object_fixup(descr->fixup_init, addr, o.state);
7073ac7fe5aSThomas Gleixner }
7083ac7fe5aSThomas Gleixner 
7093ac7fe5aSThomas Gleixner /**
7103ac7fe5aSThomas Gleixner  * debug_object_init - debug checks when an object is initialized
7113ac7fe5aSThomas Gleixner  * @addr:	address of the object
7123ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7133ac7fe5aSThomas Gleixner  */
714aedcade6SStephen Boyd void debug_object_init(void *addr, const struct debug_obj_descr *descr)
7153ac7fe5aSThomas Gleixner {
7163ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7173ac7fe5aSThomas Gleixner 		return;
7183ac7fe5aSThomas Gleixner 
7193ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 0);
7203ac7fe5aSThomas Gleixner }
721f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init);
7223ac7fe5aSThomas Gleixner 
7233ac7fe5aSThomas Gleixner /**
7243ac7fe5aSThomas Gleixner  * debug_object_init_on_stack - debug checks when an object on stack is
7253ac7fe5aSThomas Gleixner  *				initialized
7263ac7fe5aSThomas Gleixner  * @addr:	address of the object
7273ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
7283ac7fe5aSThomas Gleixner  */
729aedcade6SStephen Boyd void debug_object_init_on_stack(void *addr, const struct debug_obj_descr *descr)
7303ac7fe5aSThomas Gleixner {
7313ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
7323ac7fe5aSThomas Gleixner 		return;
7333ac7fe5aSThomas Gleixner 
7343ac7fe5aSThomas Gleixner 	__debug_object_init(addr, descr, 1);
7353ac7fe5aSThomas Gleixner }
736f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_init_on_stack);
7373ac7fe5aSThomas Gleixner 
7383ac7fe5aSThomas Gleixner /**
7393ac7fe5aSThomas Gleixner  * debug_object_activate - debug checks when an object is activated
7403ac7fe5aSThomas Gleixner  * @addr:	address of the object
7413ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
742b778ae25SPaul E. McKenney  * Returns 0 for success, -EINVAL for check failed.
7433ac7fe5aSThomas Gleixner  */
744aedcade6SStephen Boyd int debug_object_activate(void *addr, const struct debug_obj_descr *descr)
7453ac7fe5aSThomas Gleixner {
74663a75969SThomas Gleixner 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
7473ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
7483ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
7493ac7fe5aSThomas Gleixner 	unsigned long flags;
7503ac7fe5aSThomas Gleixner 
7513ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
752b778ae25SPaul E. McKenney 		return 0;
7533ac7fe5aSThomas Gleixner 
7540af462f1SThomas Gleixner 	debug_objects_fill_pool();
7550af462f1SThomas Gleixner 
7563ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
7573ac7fe5aSThomas Gleixner 
758aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
7593ac7fe5aSThomas Gleixner 
76063a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, false, true);
7619bb63626SAndrzej Hajda 	if (unlikely(!obj)) {
7629bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
7639bb63626SAndrzej Hajda 		debug_objects_oom();
7649bb63626SAndrzej Hajda 		return 0;
7659bb63626SAndrzej Hajda 	} else if (likely(!IS_ERR(obj))) {
7663ac7fe5aSThomas Gleixner 		switch (obj->state) {
7679bb63626SAndrzej Hajda 		case ODEBUG_STATE_ACTIVE:
7689bb63626SAndrzej Hajda 		case ODEBUG_STATE_DESTROYED:
7699bb63626SAndrzej Hajda 			o = *obj;
7709bb63626SAndrzej Hajda 			break;
7713ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
7723ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
7733ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_ACTIVE;
7749bb63626SAndrzej Hajda 			fallthrough;
7753ac7fe5aSThomas Gleixner 		default:
776aef9cb05SThomas Gleixner 			raw_spin_unlock_irqrestore(&db->lock, flags);
777b778ae25SPaul E. McKenney 			return 0;
7783ac7fe5aSThomas Gleixner 		}
7799bb63626SAndrzej Hajda 	}
78063a75969SThomas Gleixner 
7819bb63626SAndrzej Hajda 	raw_spin_unlock_irqrestore(&db->lock, flags);
78263a75969SThomas Gleixner 	debug_print_object(&o, "activate");
7839bb63626SAndrzej Hajda 
7849bb63626SAndrzej Hajda 	switch (o.state) {
7859bb63626SAndrzej Hajda 	case ODEBUG_STATE_ACTIVE:
7869bb63626SAndrzej Hajda 	case ODEBUG_STATE_NOTAVAILABLE:
7879bb63626SAndrzej Hajda 		if (debug_object_fixup(descr->fixup_activate, addr, o.state))
7889bb63626SAndrzej Hajda 			return 0;
7899bb63626SAndrzej Hajda 		fallthrough;
7909bb63626SAndrzej Hajda 	default:
7919bb63626SAndrzej Hajda 		return -EINVAL;
7929bb63626SAndrzej Hajda 	}
79363a75969SThomas Gleixner }
794f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_activate);
7953ac7fe5aSThomas Gleixner 
7963ac7fe5aSThomas Gleixner /**
7973ac7fe5aSThomas Gleixner  * debug_object_deactivate - debug checks when an object is deactivated
7983ac7fe5aSThomas Gleixner  * @addr:	address of the object
7993ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
8003ac7fe5aSThomas Gleixner  */
801aedcade6SStephen Boyd void debug_object_deactivate(void *addr, const struct debug_obj_descr *descr)
8023ac7fe5aSThomas Gleixner {
8039bb63626SAndrzej Hajda 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
8043ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8053ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
8063ac7fe5aSThomas Gleixner 	unsigned long flags;
8073ac7fe5aSThomas Gleixner 
8083ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8093ac7fe5aSThomas Gleixner 		return;
8103ac7fe5aSThomas Gleixner 
8113ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8123ac7fe5aSThomas Gleixner 
813aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8143ac7fe5aSThomas Gleixner 
8153ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
8163ac7fe5aSThomas Gleixner 	if (obj) {
8173ac7fe5aSThomas Gleixner 		switch (obj->state) {
8189bb63626SAndrzej Hajda 		case ODEBUG_STATE_DESTROYED:
8199bb63626SAndrzej Hajda 			break;
8203ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INIT:
8213ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_INACTIVE:
8223ac7fe5aSThomas Gleixner 		case ODEBUG_STATE_ACTIVE:
8239bb63626SAndrzej Hajda 			if (obj->astate)
8249bb63626SAndrzej Hajda 				break;
8253ac7fe5aSThomas Gleixner 			obj->state = ODEBUG_STATE_INACTIVE;
8269bb63626SAndrzej Hajda 			fallthrough;
8273ac7fe5aSThomas Gleixner 		default:
8289bb63626SAndrzej Hajda 			raw_spin_unlock_irqrestore(&db->lock, flags);
8299bb63626SAndrzej Hajda 			return;
8303ac7fe5aSThomas Gleixner 		}
8319bb63626SAndrzej Hajda 		o = *obj;
832d5f34153SWaiman Long 	}
833d5f34153SWaiman Long 
834d5f34153SWaiman Long 	raw_spin_unlock_irqrestore(&db->lock, flags);
8353ac7fe5aSThomas Gleixner 	debug_print_object(&o, "deactivate");
8363ac7fe5aSThomas Gleixner }
837f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_deactivate);
8383ac7fe5aSThomas Gleixner 
8393ac7fe5aSThomas Gleixner /**
8403ac7fe5aSThomas Gleixner  * debug_object_destroy - debug checks when an object is destroyed
8413ac7fe5aSThomas Gleixner  * @addr:	address of the object
8423ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
8433ac7fe5aSThomas Gleixner  */
844aedcade6SStephen Boyd void debug_object_destroy(void *addr, const struct debug_obj_descr *descr)
8453ac7fe5aSThomas Gleixner {
8469bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
8473ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8483ac7fe5aSThomas Gleixner 	unsigned long flags;
8493ac7fe5aSThomas Gleixner 
8503ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8513ac7fe5aSThomas Gleixner 		return;
8523ac7fe5aSThomas Gleixner 
8533ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
8543ac7fe5aSThomas Gleixner 
855aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
8563ac7fe5aSThomas Gleixner 
8573ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
8589bb63626SAndrzej Hajda 	if (!obj) {
8599bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
8609bb63626SAndrzej Hajda 		return;
8619bb63626SAndrzej Hajda 	}
8623ac7fe5aSThomas Gleixner 
8633ac7fe5aSThomas Gleixner 	switch (obj->state) {
8649bb63626SAndrzej Hajda 	case ODEBUG_STATE_ACTIVE:
8659bb63626SAndrzej Hajda 	case ODEBUG_STATE_DESTROYED:
8669bb63626SAndrzej Hajda 		break;
8673ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NONE:
8683ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INIT:
8693ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_INACTIVE:
8703ac7fe5aSThomas Gleixner 		obj->state = ODEBUG_STATE_DESTROYED;
8719bb63626SAndrzej Hajda 		fallthrough;
8723ac7fe5aSThomas Gleixner 	default:
873aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
8749bb63626SAndrzej Hajda 		return;
8759bb63626SAndrzej Hajda 	}
8769bb63626SAndrzej Hajda 
8779bb63626SAndrzej Hajda 	o = *obj;
8789bb63626SAndrzej Hajda 	raw_spin_unlock_irqrestore(&db->lock, flags);
8799bb63626SAndrzej Hajda 	debug_print_object(&o, "destroy");
8809bb63626SAndrzej Hajda 
8819bb63626SAndrzej Hajda 	if (o.state == ODEBUG_STATE_ACTIVE)
8829bb63626SAndrzej Hajda 		debug_object_fixup(descr->fixup_destroy, addr, o.state);
8833ac7fe5aSThomas Gleixner }
884f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_destroy);
8853ac7fe5aSThomas Gleixner 
8863ac7fe5aSThomas Gleixner /**
8873ac7fe5aSThomas Gleixner  * debug_object_free - debug checks when an object is freed
8883ac7fe5aSThomas Gleixner  * @addr:	address of the object
8893ac7fe5aSThomas Gleixner  * @descr:	pointer to an object specific debug description structure
8903ac7fe5aSThomas Gleixner  */
891aedcade6SStephen Boyd void debug_object_free(void *addr, const struct debug_obj_descr *descr)
8923ac7fe5aSThomas Gleixner {
8939bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
8943ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
8953ac7fe5aSThomas Gleixner 	unsigned long flags;
8963ac7fe5aSThomas Gleixner 
8973ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
8983ac7fe5aSThomas Gleixner 		return;
8993ac7fe5aSThomas Gleixner 
9003ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
9013ac7fe5aSThomas Gleixner 
902aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
9033ac7fe5aSThomas Gleixner 
9043ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
9059bb63626SAndrzej Hajda 	if (!obj) {
9069bb63626SAndrzej Hajda 		raw_spin_unlock_irqrestore(&db->lock, flags);
9079bb63626SAndrzej Hajda 		return;
9089bb63626SAndrzej Hajda 	}
9093ac7fe5aSThomas Gleixner 
9103ac7fe5aSThomas Gleixner 	switch (obj->state) {
9113ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
9129bb63626SAndrzej Hajda 		break;
9133ac7fe5aSThomas Gleixner 	default:
9143ac7fe5aSThomas Gleixner 		hlist_del(&obj->node);
915aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
9163ac7fe5aSThomas Gleixner 		free_object(obj);
917673d62ccSVegard Nossum 		return;
9183ac7fe5aSThomas Gleixner 	}
9199bb63626SAndrzej Hajda 
9209bb63626SAndrzej Hajda 	o = *obj;
921aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
9229bb63626SAndrzej Hajda 	debug_print_object(&o, "free");
9239bb63626SAndrzej Hajda 
9249bb63626SAndrzej Hajda 	debug_object_fixup(descr->fixup_free, addr, o.state);
9253ac7fe5aSThomas Gleixner }
926f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_free);
9273ac7fe5aSThomas Gleixner 
928a5d8e467SMathieu Desnoyers /**
929b84d435cSChristine Chan  * debug_object_assert_init - debug checks when object should be init-ed
930b84d435cSChristine Chan  * @addr:	address of the object
931b84d435cSChristine Chan  * @descr:	pointer to an object specific debug description structure
932b84d435cSChristine Chan  */
933aedcade6SStephen Boyd void debug_object_assert_init(void *addr, const struct debug_obj_descr *descr)
934b84d435cSChristine Chan {
93563a75969SThomas Gleixner 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
936b84d435cSChristine Chan 	struct debug_bucket *db;
937b84d435cSChristine Chan 	struct debug_obj *obj;
938b84d435cSChristine Chan 	unsigned long flags;
939b84d435cSChristine Chan 
940b84d435cSChristine Chan 	if (!debug_objects_enabled)
941b84d435cSChristine Chan 		return;
942b84d435cSChristine Chan 
9430af462f1SThomas Gleixner 	debug_objects_fill_pool();
9440af462f1SThomas Gleixner 
945b84d435cSChristine Chan 	db = get_bucket((unsigned long) addr);
946b84d435cSChristine Chan 
947b84d435cSChristine Chan 	raw_spin_lock_irqsave(&db->lock, flags);
94863a75969SThomas Gleixner 	obj = lookup_object_or_alloc(addr, db, descr, false, true);
949b84d435cSChristine Chan 	raw_spin_unlock_irqrestore(&db->lock, flags);
95063a75969SThomas Gleixner 	if (likely(!IS_ERR_OR_NULL(obj)))
95163a75969SThomas Gleixner 		return;
95263a75969SThomas Gleixner 
95363a75969SThomas Gleixner 	/* If NULL the allocation has hit OOM */
95463a75969SThomas Gleixner 	if (!obj) {
95563a75969SThomas Gleixner 		debug_objects_oom();
956b84d435cSChristine Chan 		return;
957b84d435cSChristine Chan 	}
958b84d435cSChristine Chan 
95963a75969SThomas Gleixner 	/* Object is neither tracked nor static. It's not initialized. */
96063a75969SThomas Gleixner 	debug_print_object(&o, "assert_init");
96163a75969SThomas Gleixner 	debug_object_fixup(descr->fixup_assert_init, addr, ODEBUG_STATE_NOTAVAILABLE);
962b84d435cSChristine Chan }
963f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_assert_init);
964b84d435cSChristine Chan 
965b84d435cSChristine Chan /**
966a5d8e467SMathieu Desnoyers  * debug_object_active_state - debug checks object usage state machine
967a5d8e467SMathieu Desnoyers  * @addr:	address of the object
968a5d8e467SMathieu Desnoyers  * @descr:	pointer to an object specific debug description structure
969a5d8e467SMathieu Desnoyers  * @expect:	expected state
970a5d8e467SMathieu Desnoyers  * @next:	state to move to if expected state is found
971a5d8e467SMathieu Desnoyers  */
972a5d8e467SMathieu Desnoyers void
973aedcade6SStephen Boyd debug_object_active_state(void *addr, const struct debug_obj_descr *descr,
974a5d8e467SMathieu Desnoyers 			  unsigned int expect, unsigned int next)
975a5d8e467SMathieu Desnoyers {
9769bb63626SAndrzej Hajda 	struct debug_obj o = { .object = addr, .state = ODEBUG_STATE_NOTAVAILABLE, .descr = descr };
977a5d8e467SMathieu Desnoyers 	struct debug_bucket *db;
978a5d8e467SMathieu Desnoyers 	struct debug_obj *obj;
979a5d8e467SMathieu Desnoyers 	unsigned long flags;
980a5d8e467SMathieu Desnoyers 
981a5d8e467SMathieu Desnoyers 	if (!debug_objects_enabled)
982a5d8e467SMathieu Desnoyers 		return;
983a5d8e467SMathieu Desnoyers 
984a5d8e467SMathieu Desnoyers 	db = get_bucket((unsigned long) addr);
985a5d8e467SMathieu Desnoyers 
986a5d8e467SMathieu Desnoyers 	raw_spin_lock_irqsave(&db->lock, flags);
987a5d8e467SMathieu Desnoyers 
988a5d8e467SMathieu Desnoyers 	obj = lookup_object(addr, db);
989a5d8e467SMathieu Desnoyers 	if (obj) {
990a5d8e467SMathieu Desnoyers 		switch (obj->state) {
991a5d8e467SMathieu Desnoyers 		case ODEBUG_STATE_ACTIVE:
9929bb63626SAndrzej Hajda 			if (obj->astate != expect)
993a5d8e467SMathieu Desnoyers 				break;
9949bb63626SAndrzej Hajda 			obj->astate = next;
9959bb63626SAndrzej Hajda 			raw_spin_unlock_irqrestore(&db->lock, flags);
9969bb63626SAndrzej Hajda 			return;
997a5d8e467SMathieu Desnoyers 		default:
998a5d8e467SMathieu Desnoyers 			break;
999a5d8e467SMathieu Desnoyers 		}
10009bb63626SAndrzej Hajda 		o = *obj;
1001d5f34153SWaiman Long 	}
1002d5f34153SWaiman Long 
1003d5f34153SWaiman Long 	raw_spin_unlock_irqrestore(&db->lock, flags);
1004a5d8e467SMathieu Desnoyers 	debug_print_object(&o, "active_state");
1005a5d8e467SMathieu Desnoyers }
1006f8ff04e2SChris Wilson EXPORT_SYMBOL_GPL(debug_object_active_state);
1007a5d8e467SMathieu Desnoyers 
10083ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
10093ac7fe5aSThomas Gleixner static void __debug_check_no_obj_freed(const void *address, unsigned long size)
10103ac7fe5aSThomas Gleixner {
10113ac7fe5aSThomas Gleixner 	unsigned long flags, oaddr, saddr, eaddr, paddr, chunks;
10129bb63626SAndrzej Hajda 	int cnt, objs_checked = 0;
10139bb63626SAndrzej Hajda 	struct debug_obj *obj, o;
10143ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
10151ea9b98bSYang Shi 	struct hlist_node *tmp;
10163ac7fe5aSThomas Gleixner 
10173ac7fe5aSThomas Gleixner 	saddr = (unsigned long) address;
10183ac7fe5aSThomas Gleixner 	eaddr = saddr + size;
10193ac7fe5aSThomas Gleixner 	paddr = saddr & ODEBUG_CHUNK_MASK;
10203ac7fe5aSThomas Gleixner 	chunks = ((eaddr - paddr) + (ODEBUG_CHUNK_SIZE - 1));
10213ac7fe5aSThomas Gleixner 	chunks >>= ODEBUG_CHUNK_SHIFT;
10223ac7fe5aSThomas Gleixner 
10233ac7fe5aSThomas Gleixner 	for (;chunks > 0; chunks--, paddr += ODEBUG_CHUNK_SIZE) {
10243ac7fe5aSThomas Gleixner 		db = get_bucket(paddr);
10253ac7fe5aSThomas Gleixner 
10263ac7fe5aSThomas Gleixner repeat:
10273ac7fe5aSThomas Gleixner 		cnt = 0;
1028aef9cb05SThomas Gleixner 		raw_spin_lock_irqsave(&db->lock, flags);
1029b67bfe0dSSasha Levin 		hlist_for_each_entry_safe(obj, tmp, &db->list, node) {
10303ac7fe5aSThomas Gleixner 			cnt++;
10313ac7fe5aSThomas Gleixner 			oaddr = (unsigned long) obj->object;
10323ac7fe5aSThomas Gleixner 			if (oaddr < saddr || oaddr >= eaddr)
10333ac7fe5aSThomas Gleixner 				continue;
10343ac7fe5aSThomas Gleixner 
10353ac7fe5aSThomas Gleixner 			switch (obj->state) {
10363ac7fe5aSThomas Gleixner 			case ODEBUG_STATE_ACTIVE:
10379bb63626SAndrzej Hajda 				o = *obj;
1038aef9cb05SThomas Gleixner 				raw_spin_unlock_irqrestore(&db->lock, flags);
10399bb63626SAndrzej Hajda 				debug_print_object(&o, "free");
10409bb63626SAndrzej Hajda 				debug_object_fixup(o.descr->fixup_free, (void *)oaddr, o.state);
10413ac7fe5aSThomas Gleixner 				goto repeat;
10423ac7fe5aSThomas Gleixner 			default:
10433ac7fe5aSThomas Gleixner 				hlist_del(&obj->node);
1044a7344a68SWaiman Long 				__free_object(obj);
10453ac7fe5aSThomas Gleixner 				break;
10463ac7fe5aSThomas Gleixner 			}
10473ac7fe5aSThomas Gleixner 		}
1048aef9cb05SThomas Gleixner 		raw_spin_unlock_irqrestore(&db->lock, flags);
1049673d62ccSVegard Nossum 
10503ac7fe5aSThomas Gleixner 		if (cnt > debug_objects_maxchain)
10513ac7fe5aSThomas Gleixner 			debug_objects_maxchain = cnt;
1052bd9dcd04SYang Shi 
1053bd9dcd04SYang Shi 		objs_checked += cnt;
10543ac7fe5aSThomas Gleixner 	}
1055bd9dcd04SYang Shi 
1056bd9dcd04SYang Shi 	if (objs_checked > debug_objects_maxchecked)
1057bd9dcd04SYang Shi 		debug_objects_maxchecked = objs_checked;
10581ea9b98bSYang Shi 
10591ea9b98bSYang Shi 	/* Schedule work to actually kmem_cache_free() objects */
1060e18328ffSThomas Gleixner 	if (!READ_ONCE(obj_freeing) && pool_count(&pool_to_free)) {
1061a7344a68SWaiman Long 		WRITE_ONCE(obj_freeing, true);
1062a7344a68SWaiman Long 		schedule_delayed_work(&debug_obj_work, ODEBUG_FREE_WORK_DELAY);
1063a7344a68SWaiman Long 	}
10643ac7fe5aSThomas Gleixner }
10653ac7fe5aSThomas Gleixner 
10663ac7fe5aSThomas Gleixner void debug_check_no_obj_freed(const void *address, unsigned long size)
10673ac7fe5aSThomas Gleixner {
10683ac7fe5aSThomas Gleixner 	if (debug_objects_enabled)
10693ac7fe5aSThomas Gleixner 		__debug_check_no_obj_freed(address, size);
10703ac7fe5aSThomas Gleixner }
10713ac7fe5aSThomas Gleixner #endif
10723ac7fe5aSThomas Gleixner 
10733ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_FS
10743ac7fe5aSThomas Gleixner 
10753ac7fe5aSThomas Gleixner static int debug_stats_show(struct seq_file *m, void *v)
10763ac7fe5aSThomas Gleixner {
1077d86998b1SWaiman Long 	int cpu, obj_percpu_free = 0;
1078d86998b1SWaiman Long 
1079d86998b1SWaiman Long 	for_each_possible_cpu(cpu)
108018b8afcbSThomas Gleixner 		obj_percpu_free += per_cpu(pool_pcpu.cnt, cpu);
1081d86998b1SWaiman Long 
10823ac7fe5aSThomas Gleixner 	seq_printf(m, "max_chain     :%d\n", debug_objects_maxchain);
1083bd9dcd04SYang Shi 	seq_printf(m, "max_checked   :%d\n", debug_objects_maxchecked);
10843ac7fe5aSThomas Gleixner 	seq_printf(m, "warnings      :%d\n", debug_objects_warnings);
10853ac7fe5aSThomas Gleixner 	seq_printf(m, "fixups        :%d\n", debug_objects_fixups);
1086e18328ffSThomas Gleixner 	seq_printf(m, "pool_free     :%d\n", pool_count(&pool_global) + obj_percpu_free);
1087d86998b1SWaiman Long 	seq_printf(m, "pool_pcp_free :%d\n", obj_percpu_free);
10883ac7fe5aSThomas Gleixner 	seq_printf(m, "pool_min_free :%d\n", obj_pool_min_free);
1089d86998b1SWaiman Long 	seq_printf(m, "pool_used     :%d\n", obj_pool_used - obj_percpu_free);
10903ac7fe5aSThomas Gleixner 	seq_printf(m, "pool_max_used :%d\n", obj_pool_max_used);
1091e18328ffSThomas Gleixner 	seq_printf(m, "on_free_list  :%d\n", pool_count(&pool_to_free));
10920cad93c3SWaiman Long 	seq_printf(m, "objs_allocated:%d\n", debug_objects_allocated);
10930cad93c3SWaiman Long 	seq_printf(m, "objs_freed    :%d\n", debug_objects_freed);
10943ac7fe5aSThomas Gleixner 	return 0;
10953ac7fe5aSThomas Gleixner }
10960f85c480SQinglang Miao DEFINE_SHOW_ATTRIBUTE(debug_stats);
10973ac7fe5aSThomas Gleixner 
10983ac7fe5aSThomas Gleixner static int __init debug_objects_init_debugfs(void)
10993ac7fe5aSThomas Gleixner {
1100fecb0d95SGreg Kroah-Hartman 	struct dentry *dbgdir;
11013ac7fe5aSThomas Gleixner 
11023ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
11033ac7fe5aSThomas Gleixner 		return 0;
11043ac7fe5aSThomas Gleixner 
11053ac7fe5aSThomas Gleixner 	dbgdir = debugfs_create_dir("debug_objects", NULL);
11063ac7fe5aSThomas Gleixner 
1107fecb0d95SGreg Kroah-Hartman 	debugfs_create_file("stats", 0444, dbgdir, NULL, &debug_stats_fops);
11083ac7fe5aSThomas Gleixner 
11093ac7fe5aSThomas Gleixner 	return 0;
11103ac7fe5aSThomas Gleixner }
11113ac7fe5aSThomas Gleixner __initcall(debug_objects_init_debugfs);
11123ac7fe5aSThomas Gleixner 
11133ac7fe5aSThomas Gleixner #else
11143ac7fe5aSThomas Gleixner static inline void debug_objects_init_debugfs(void) { }
11153ac7fe5aSThomas Gleixner #endif
11163ac7fe5aSThomas Gleixner 
11173ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_SELFTEST
11183ac7fe5aSThomas Gleixner 
11193ac7fe5aSThomas Gleixner /* Random data structure for the self test */
11203ac7fe5aSThomas Gleixner struct self_test {
11213ac7fe5aSThomas Gleixner 	unsigned long	dummy1[6];
11223ac7fe5aSThomas Gleixner 	int		static_init;
11233ac7fe5aSThomas Gleixner 	unsigned long	dummy2[3];
11243ac7fe5aSThomas Gleixner };
11253ac7fe5aSThomas Gleixner 
1126aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test;
11273ac7fe5aSThomas Gleixner 
1128b9fdac7fSDu, Changbin static bool __init is_static_object(void *addr)
1129b9fdac7fSDu, Changbin {
1130b9fdac7fSDu, Changbin 	struct self_test *obj = addr;
1131b9fdac7fSDu, Changbin 
1132b9fdac7fSDu, Changbin 	return obj->static_init;
1133b9fdac7fSDu, Changbin }
1134b9fdac7fSDu, Changbin 
11353ac7fe5aSThomas Gleixner /*
11363ac7fe5aSThomas Gleixner  * fixup_init is called when:
11373ac7fe5aSThomas Gleixner  * - an active object is initialized
11383ac7fe5aSThomas Gleixner  */
1139b1e4d9d8SDu, Changbin static bool __init fixup_init(void *addr, enum debug_obj_state state)
11403ac7fe5aSThomas Gleixner {
11413ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11423ac7fe5aSThomas Gleixner 
11433ac7fe5aSThomas Gleixner 	switch (state) {
11443ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11453ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11463ac7fe5aSThomas Gleixner 		debug_object_init(obj, &descr_type_test);
1147b1e4d9d8SDu, Changbin 		return true;
11483ac7fe5aSThomas Gleixner 	default:
1149b1e4d9d8SDu, Changbin 		return false;
11503ac7fe5aSThomas Gleixner 	}
11513ac7fe5aSThomas Gleixner }
11523ac7fe5aSThomas Gleixner 
11533ac7fe5aSThomas Gleixner /*
11543ac7fe5aSThomas Gleixner  * fixup_activate is called when:
11553ac7fe5aSThomas Gleixner  * - an active object is activated
1156b9fdac7fSDu, Changbin  * - an unknown non-static object is activated
11573ac7fe5aSThomas Gleixner  */
1158b1e4d9d8SDu, Changbin static bool __init fixup_activate(void *addr, enum debug_obj_state state)
11593ac7fe5aSThomas Gleixner {
11603ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11613ac7fe5aSThomas Gleixner 
11623ac7fe5aSThomas Gleixner 	switch (state) {
11633ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_NOTAVAILABLE:
1164b1e4d9d8SDu, Changbin 		return true;
11653ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11663ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11673ac7fe5aSThomas Gleixner 		debug_object_activate(obj, &descr_type_test);
1168b1e4d9d8SDu, Changbin 		return true;
11693ac7fe5aSThomas Gleixner 
11703ac7fe5aSThomas Gleixner 	default:
1171b1e4d9d8SDu, Changbin 		return false;
11723ac7fe5aSThomas Gleixner 	}
11733ac7fe5aSThomas Gleixner }
11743ac7fe5aSThomas Gleixner 
11753ac7fe5aSThomas Gleixner /*
11763ac7fe5aSThomas Gleixner  * fixup_destroy is called when:
11773ac7fe5aSThomas Gleixner  * - an active object is destroyed
11783ac7fe5aSThomas Gleixner  */
1179b1e4d9d8SDu, Changbin static bool __init fixup_destroy(void *addr, enum debug_obj_state state)
11803ac7fe5aSThomas Gleixner {
11813ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
11823ac7fe5aSThomas Gleixner 
11833ac7fe5aSThomas Gleixner 	switch (state) {
11843ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
11853ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
11863ac7fe5aSThomas Gleixner 		debug_object_destroy(obj, &descr_type_test);
1187b1e4d9d8SDu, Changbin 		return true;
11883ac7fe5aSThomas Gleixner 	default:
1189b1e4d9d8SDu, Changbin 		return false;
11903ac7fe5aSThomas Gleixner 	}
11913ac7fe5aSThomas Gleixner }
11923ac7fe5aSThomas Gleixner 
11933ac7fe5aSThomas Gleixner /*
11943ac7fe5aSThomas Gleixner  * fixup_free is called when:
11953ac7fe5aSThomas Gleixner  * - an active object is freed
11963ac7fe5aSThomas Gleixner  */
1197b1e4d9d8SDu, Changbin static bool __init fixup_free(void *addr, enum debug_obj_state state)
11983ac7fe5aSThomas Gleixner {
11993ac7fe5aSThomas Gleixner 	struct self_test *obj = addr;
12003ac7fe5aSThomas Gleixner 
12013ac7fe5aSThomas Gleixner 	switch (state) {
12023ac7fe5aSThomas Gleixner 	case ODEBUG_STATE_ACTIVE:
12033ac7fe5aSThomas Gleixner 		debug_object_deactivate(obj, &descr_type_test);
12043ac7fe5aSThomas Gleixner 		debug_object_free(obj, &descr_type_test);
1205b1e4d9d8SDu, Changbin 		return true;
12063ac7fe5aSThomas Gleixner 	default:
1207b1e4d9d8SDu, Changbin 		return false;
12083ac7fe5aSThomas Gleixner 	}
12093ac7fe5aSThomas Gleixner }
12103ac7fe5aSThomas Gleixner 
12111fb2f77cSHenrik Kretzschmar static int __init
12123ac7fe5aSThomas Gleixner check_results(void *addr, enum debug_obj_state state, int fixups, int warnings)
12133ac7fe5aSThomas Gleixner {
12143ac7fe5aSThomas Gleixner 	struct debug_bucket *db;
12153ac7fe5aSThomas Gleixner 	struct debug_obj *obj;
12163ac7fe5aSThomas Gleixner 	unsigned long flags;
12173ac7fe5aSThomas Gleixner 	int res = -EINVAL;
12183ac7fe5aSThomas Gleixner 
12193ac7fe5aSThomas Gleixner 	db = get_bucket((unsigned long) addr);
12203ac7fe5aSThomas Gleixner 
1221aef9cb05SThomas Gleixner 	raw_spin_lock_irqsave(&db->lock, flags);
12223ac7fe5aSThomas Gleixner 
12233ac7fe5aSThomas Gleixner 	obj = lookup_object(addr, db);
12243ac7fe5aSThomas Gleixner 	if (!obj && state != ODEBUG_STATE_NONE) {
12255cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest object not found\n");
12263ac7fe5aSThomas Gleixner 		goto out;
12273ac7fe5aSThomas Gleixner 	}
12283ac7fe5aSThomas Gleixner 	if (obj && obj->state != state) {
12295cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest wrong state: %d != %d\n",
12303ac7fe5aSThomas Gleixner 		       obj->state, state);
12313ac7fe5aSThomas Gleixner 		goto out;
12323ac7fe5aSThomas Gleixner 	}
12333ac7fe5aSThomas Gleixner 	if (fixups != debug_objects_fixups) {
12345cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest fixups failed %d != %d\n",
12353ac7fe5aSThomas Gleixner 		       fixups, debug_objects_fixups);
12363ac7fe5aSThomas Gleixner 		goto out;
12373ac7fe5aSThomas Gleixner 	}
12383ac7fe5aSThomas Gleixner 	if (warnings != debug_objects_warnings) {
12395cd2b459SArjan van de Ven 		WARN(1, KERN_ERR "ODEBUG: selftest warnings failed %d != %d\n",
12403ac7fe5aSThomas Gleixner 		       warnings, debug_objects_warnings);
12413ac7fe5aSThomas Gleixner 		goto out;
12423ac7fe5aSThomas Gleixner 	}
12433ac7fe5aSThomas Gleixner 	res = 0;
12443ac7fe5aSThomas Gleixner out:
1245aef9cb05SThomas Gleixner 	raw_spin_unlock_irqrestore(&db->lock, flags);
12463ac7fe5aSThomas Gleixner 	if (res)
1247661cc28bSThomas Gleixner 		debug_objects_enabled = false;
12483ac7fe5aSThomas Gleixner 	return res;
12493ac7fe5aSThomas Gleixner }
12503ac7fe5aSThomas Gleixner 
1251aedcade6SStephen Boyd static __initconst const struct debug_obj_descr descr_type_test = {
12523ac7fe5aSThomas Gleixner 	.name			= "selftest",
1253b9fdac7fSDu, Changbin 	.is_static_object	= is_static_object,
12543ac7fe5aSThomas Gleixner 	.fixup_init		= fixup_init,
12553ac7fe5aSThomas Gleixner 	.fixup_activate		= fixup_activate,
12563ac7fe5aSThomas Gleixner 	.fixup_destroy		= fixup_destroy,
12573ac7fe5aSThomas Gleixner 	.fixup_free		= fixup_free,
12583ac7fe5aSThomas Gleixner };
12593ac7fe5aSThomas Gleixner 
12603ac7fe5aSThomas Gleixner static __initdata struct self_test obj = { .static_init = 0 };
12613ac7fe5aSThomas Gleixner 
126255fb412eSThomas Gleixner static bool __init debug_objects_selftest(void)
12633ac7fe5aSThomas Gleixner {
12643ac7fe5aSThomas Gleixner 	int fixups, oldfixups, warnings, oldwarnings;
12653ac7fe5aSThomas Gleixner 	unsigned long flags;
12663ac7fe5aSThomas Gleixner 
12673ac7fe5aSThomas Gleixner 	local_irq_save(flags);
12683ac7fe5aSThomas Gleixner 
12693ac7fe5aSThomas Gleixner 	fixups = oldfixups = debug_objects_fixups;
12703ac7fe5aSThomas Gleixner 	warnings = oldwarnings = debug_objects_warnings;
12713ac7fe5aSThomas Gleixner 	descr_test = &descr_type_test;
12723ac7fe5aSThomas Gleixner 
12733ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12743ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
12753ac7fe5aSThomas Gleixner 		goto out;
12763ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12773ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
12783ac7fe5aSThomas Gleixner 		goto out;
12793ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12803ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, ++fixups, ++warnings))
12813ac7fe5aSThomas Gleixner 		goto out;
12823ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
12833ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INACTIVE, fixups, warnings))
12843ac7fe5aSThomas Gleixner 		goto out;
12853ac7fe5aSThomas Gleixner 	debug_object_destroy(&obj, &descr_type_test);
12863ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, warnings))
12873ac7fe5aSThomas Gleixner 		goto out;
12883ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
12893ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12903ac7fe5aSThomas Gleixner 		goto out;
12913ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
12923ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12933ac7fe5aSThomas Gleixner 		goto out;
12943ac7fe5aSThomas Gleixner 	debug_object_deactivate(&obj, &descr_type_test);
12953ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_DESTROYED, fixups, ++warnings))
12963ac7fe5aSThomas Gleixner 		goto out;
12973ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
12983ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
12993ac7fe5aSThomas Gleixner 		goto out;
13003ac7fe5aSThomas Gleixner 
13013ac7fe5aSThomas Gleixner 	obj.static_init = 1;
13023ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13039f78ff00SStephen Boyd 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
13043ac7fe5aSThomas Gleixner 		goto out;
13053ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
13063ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, ++fixups, ++warnings))
13073ac7fe5aSThomas Gleixner 		goto out;
13083ac7fe5aSThomas Gleixner 	debug_object_free(&obj, &descr_type_test);
13093ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, fixups, warnings))
13103ac7fe5aSThomas Gleixner 		goto out;
13113ac7fe5aSThomas Gleixner 
13123ac7fe5aSThomas Gleixner #ifdef CONFIG_DEBUG_OBJECTS_FREE
13133ac7fe5aSThomas Gleixner 	debug_object_init(&obj, &descr_type_test);
13143ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_INIT, fixups, warnings))
13153ac7fe5aSThomas Gleixner 		goto out;
13163ac7fe5aSThomas Gleixner 	debug_object_activate(&obj, &descr_type_test);
13173ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_ACTIVE, fixups, warnings))
13183ac7fe5aSThomas Gleixner 		goto out;
13193ac7fe5aSThomas Gleixner 	__debug_check_no_obj_freed(&obj, sizeof(obj));
13203ac7fe5aSThomas Gleixner 	if (check_results(&obj, ODEBUG_STATE_NONE, ++fixups, ++warnings))
13213ac7fe5aSThomas Gleixner 		goto out;
13223ac7fe5aSThomas Gleixner #endif
1323719e4843SFabian Frederick 	pr_info("selftest passed\n");
13243ac7fe5aSThomas Gleixner 
13253ac7fe5aSThomas Gleixner out:
13263ac7fe5aSThomas Gleixner 	debug_objects_fixups = oldfixups;
13273ac7fe5aSThomas Gleixner 	debug_objects_warnings = oldwarnings;
13283ac7fe5aSThomas Gleixner 	descr_test = NULL;
13293ac7fe5aSThomas Gleixner 
13303ac7fe5aSThomas Gleixner 	local_irq_restore(flags);
1331661cc28bSThomas Gleixner 	return debug_objects_enabled;
13323ac7fe5aSThomas Gleixner }
13333ac7fe5aSThomas Gleixner #else
133455fb412eSThomas Gleixner static inline bool debug_objects_selftest(void) { return true; }
13353ac7fe5aSThomas Gleixner #endif
13363ac7fe5aSThomas Gleixner 
13373ac7fe5aSThomas Gleixner /*
13383ac7fe5aSThomas Gleixner  * Called during early boot to initialize the hash buckets and link
13393ac7fe5aSThomas Gleixner  * the static object pool objects into the poll list. After this call
13403ac7fe5aSThomas Gleixner  * the object tracker is fully operational.
13413ac7fe5aSThomas Gleixner  */
13423ac7fe5aSThomas Gleixner void __init debug_objects_early_init(void)
13433ac7fe5aSThomas Gleixner {
13443ac7fe5aSThomas Gleixner 	int i;
13453ac7fe5aSThomas Gleixner 
13463ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++)
1347aef9cb05SThomas Gleixner 		raw_spin_lock_init(&obj_hash[i].lock);
13483ac7fe5aSThomas Gleixner 
1349cb58d190SThomas Gleixner 	/* Keep early boot simple and add everything to the boot list */
13503ac7fe5aSThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i++)
1351cb58d190SThomas Gleixner 		hlist_add_head(&obj_static_pool[i].node, &pool_boot);
13523ac7fe5aSThomas Gleixner }
13533ac7fe5aSThomas Gleixner 
13543ac7fe5aSThomas Gleixner /*
135555fb412eSThomas Gleixner  * Convert the statically allocated objects to dynamic ones.
135655fb412eSThomas Gleixner  * debug_objects_mem_init() is called early so only one CPU is up and
135755fb412eSThomas Gleixner  * interrupts are disabled, which means it is safe to replace the active
135855fb412eSThomas Gleixner  * object references.
13591be1cb7bSThomas Gleixner  */
136055fb412eSThomas Gleixner static bool __init debug_objects_replace_static_objects(struct kmem_cache *cache)
13611be1cb7bSThomas Gleixner {
13621be1cb7bSThomas Gleixner 	struct debug_bucket *db = obj_hash;
136355fb412eSThomas Gleixner 	struct hlist_node *tmp;
1364aebbfe07SThomas Gleixner 	struct debug_obj *obj;
13651be1cb7bSThomas Gleixner 	HLIST_HEAD(objects);
1366241463f4SThomas Gleixner 	int i;
13671be1cb7bSThomas Gleixner 
1368aebbfe07SThomas Gleixner 	for (i = 0; i < ODEBUG_POOL_SIZE; i += ODEBUG_BATCH_SIZE) {
1369aebbfe07SThomas Gleixner 		if (!kmem_alloc_batch(&objects, cache, GFP_KERNEL))
13701be1cb7bSThomas Gleixner 			goto free;
1371aebbfe07SThomas Gleixner 		pool_push_batch(&pool_global, &objects);
13721be1cb7bSThomas Gleixner 	}
13731be1cb7bSThomas Gleixner 
1374aebbfe07SThomas Gleixner 	/* Disconnect the boot pool. */
1375cb58d190SThomas Gleixner 	pool_boot.first = NULL;
13761be1cb7bSThomas Gleixner 
13771be1cb7bSThomas Gleixner 	/* Replace the active object references */
13781be1cb7bSThomas Gleixner 	for (i = 0; i < ODEBUG_HASH_SIZE; i++, db++) {
13791be1cb7bSThomas Gleixner 		hlist_move_list(&db->list, &objects);
13801be1cb7bSThomas Gleixner 
1381b67bfe0dSSasha Levin 		hlist_for_each_entry(obj, &objects, node) {
1382aebbfe07SThomas Gleixner 			struct debug_obj *new = pcpu_alloc();
1383aebbfe07SThomas Gleixner 
13841be1cb7bSThomas Gleixner 			/* copy object data */
13851be1cb7bSThomas Gleixner 			*new = *obj;
13861be1cb7bSThomas Gleixner 			hlist_add_head(&new->node, &db->list);
13871be1cb7bSThomas Gleixner 		}
13881be1cb7bSThomas Gleixner 	}
138955fb412eSThomas Gleixner 	return true;
13901be1cb7bSThomas Gleixner free:
139149a5cb82SThomas Gleixner 	/* Can't use free_object_list() as the cache is not populated yet */
1392aebbfe07SThomas Gleixner 	hlist_for_each_entry_safe(obj, tmp, &pool_global.objects, node) {
13931be1cb7bSThomas Gleixner 		hlist_del(&obj->node);
139455fb412eSThomas Gleixner 		kmem_cache_free(cache, obj);
13951be1cb7bSThomas Gleixner 	}
139655fb412eSThomas Gleixner 	return false;
13971be1cb7bSThomas Gleixner }
13981be1cb7bSThomas Gleixner 
13991be1cb7bSThomas Gleixner /*
14003ac7fe5aSThomas Gleixner  * Called after the kmem_caches are functional to setup a dedicated
14013ac7fe5aSThomas Gleixner  * cache pool, which has the SLAB_DEBUG_OBJECTS flag set. This flag
14023ac7fe5aSThomas Gleixner  * prevents that the debug code is called on kmem_cache_free() for the
14033ac7fe5aSThomas Gleixner  * debug tracker objects to avoid recursive calls.
14043ac7fe5aSThomas Gleixner  */
14053ac7fe5aSThomas Gleixner void __init debug_objects_mem_init(void)
14063ac7fe5aSThomas Gleixner {
140755fb412eSThomas Gleixner 	struct kmem_cache *cache;
14083f397bf9SThomas Gleixner 	int extras;
1409d86998b1SWaiman Long 
14103ac7fe5aSThomas Gleixner 	if (!debug_objects_enabled)
14113ac7fe5aSThomas Gleixner 		return;
14123ac7fe5aSThomas Gleixner 
141355fb412eSThomas Gleixner 	if (!debug_objects_selftest())
1414eabb7f1aSwuchi 		return;
141555fb412eSThomas Gleixner 
141655fb412eSThomas Gleixner 	cache = kmem_cache_create("debug_objects_cache", sizeof (struct debug_obj), 0,
141755fb412eSThomas Gleixner 				  SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE, NULL);
141855fb412eSThomas Gleixner 
141955fb412eSThomas Gleixner 	if (!cache || !debug_objects_replace_static_objects(cache)) {
1420661cc28bSThomas Gleixner 		debug_objects_enabled = false;
142155fb412eSThomas Gleixner 		pr_warn("Out of memory.\n");
142255fb412eSThomas Gleixner 		return;
142355fb412eSThomas Gleixner 	}
142455fb412eSThomas Gleixner 
142555fb412eSThomas Gleixner 	/*
142655fb412eSThomas Gleixner 	 * Adjust the thresholds for allocating and freeing objects
142755fb412eSThomas Gleixner 	 * according to the number of possible CPUs available in the
142855fb412eSThomas Gleixner 	 * system.
142955fb412eSThomas Gleixner 	 */
143055fb412eSThomas Gleixner 	extras = num_possible_cpus() * ODEBUG_BATCH_SIZE;
143196a9a042SThomas Gleixner 	pool_global.max_cnt += extras;
143296a9a042SThomas Gleixner 	pool_global.min_cnt += extras;
143355fb412eSThomas Gleixner 
143455fb412eSThomas Gleixner 	/* Everything worked. Expose the cache */
143555fb412eSThomas Gleixner 	obj_cache = cache;
143614077b9eSThomas Gleixner 	static_branch_enable(&obj_cache_enabled);
1437634d61f4SWaiman Long 
143888451f2cSZqiang #ifdef CONFIG_HOTPLUG_CPU
143988451f2cSZqiang 	cpuhp_setup_state_nocalls(CPUHP_DEBUG_OBJ_DEAD, "object:offline", NULL,
144088451f2cSZqiang 				  object_cpu_offline);
144188451f2cSZqiang #endif
144255fb412eSThomas Gleixner 	return;
14433ac7fe5aSThomas Gleixner }
1444