uma_dbg.c revision 295221
1/*-
2 * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <jeff@FreeBSD.org>
3 * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice unmodified, this list of conditions, and the following
11 *    disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28/*
29 * uma_dbg.c	Debugging features for UMA users
30 *
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: head/sys/vm/uma_dbg.c 295221 2016-02-03 22:02:36Z glebius $");
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/bitset.h>
39#include <sys/kernel.h>
40#include <sys/types.h>
41#include <sys/queue.h>
42#include <sys/lock.h>
43#include <sys/mutex.h>
44#include <sys/malloc.h>
45
46#include <vm/vm.h>
47#include <vm/vm_object.h>
48#include <vm/vm_page.h>
49#include <vm/uma.h>
50#include <vm/uma_int.h>
51#include <vm/uma_dbg.h>
52
53static const uint32_t uma_junk = 0xdeadc0de;
54
55/*
56 * Checks an item to make sure it hasn't been overwritten since it was freed,
57 * prior to subsequent reallocation.
58 *
59 * Complies with standard ctor arg/return
60 *
61 */
62int
63trash_ctor(void *mem, int size, void *arg, int flags)
64{
65	int cnt;
66	uint32_t *p;
67
68	cnt = size / sizeof(uma_junk);
69
70	for (p = mem; cnt > 0; cnt--, p++)
71		if (*p != uma_junk) {
72#ifdef INVARIANTS
73			panic("Memory modified after free %p(%d) val=%x @ %p\n",
74			    mem, size, *p, p);
75#else
76			printf("Memory modified after free %p(%d) val=%x @ %p\n",
77			    mem, size, *p, p);
78#endif
79			return (0);
80		}
81	return (0);
82}
83
84/*
85 * Fills an item with predictable garbage
86 *
87 * Complies with standard dtor arg/return
88 *
89 */
90void
91trash_dtor(void *mem, int size, void *arg)
92{
93	int cnt;
94	uint32_t *p;
95
96	cnt = size / sizeof(uma_junk);
97
98	for (p = mem; cnt > 0; cnt--, p++)
99		*p = uma_junk;
100}
101
102/*
103 * Fills an item with predictable garbage
104 *
105 * Complies with standard init arg/return
106 *
107 */
108int
109trash_init(void *mem, int size, int flags)
110{
111	trash_dtor(mem, size, NULL);
112	return (0);
113}
114
115/*
116 * Checks an item to make sure it hasn't been overwritten since it was freed.
117 *
118 * Complies with standard fini arg/return
119 *
120 */
121void
122trash_fini(void *mem, int size)
123{
124	(void)trash_ctor(mem, size, NULL, 0);
125}
126
127int
128mtrash_ctor(void *mem, int size, void *arg, int flags)
129{
130	struct malloc_type **ksp;
131	uint32_t *p = mem;
132	int cnt;
133
134	size -= sizeof(struct malloc_type *);
135	ksp = (struct malloc_type **)mem;
136	ksp += size / sizeof(struct malloc_type *);
137	cnt = size / sizeof(uma_junk);
138
139	for (p = mem; cnt > 0; cnt--, p++)
140		if (*p != uma_junk) {
141			printf("Memory modified after free %p(%d) val=%x @ %p\n",
142			    mem, size, *p, p);
143			panic("Most recently used by %s\n", (*ksp == NULL)?
144			    "none" : (*ksp)->ks_shortdesc);
145		}
146	return (0);
147}
148
149/*
150 * Fills an item with predictable garbage
151 *
152 * Complies with standard dtor arg/return
153 *
154 */
155void
156mtrash_dtor(void *mem, int size, void *arg)
157{
158	int cnt;
159	uint32_t *p;
160
161	size -= sizeof(struct malloc_type *);
162	cnt = size / sizeof(uma_junk);
163
164	for (p = mem; cnt > 0; cnt--, p++)
165		*p = uma_junk;
166}
167
168/*
169 * Fills an item with predictable garbage
170 *
171 * Complies with standard init arg/return
172 *
173 */
174int
175mtrash_init(void *mem, int size, int flags)
176{
177	struct malloc_type **ksp;
178
179	mtrash_dtor(mem, size, NULL);
180
181	ksp = (struct malloc_type **)mem;
182	ksp += (size / sizeof(struct malloc_type *)) - 1;
183	*ksp = NULL;
184	return (0);
185}
186
187/*
188 * Checks an item to make sure it hasn't been overwritten since it was freed,
189 * prior to freeing it back to available memory.
190 *
191 * Complies with standard fini arg/return
192 *
193 */
194void
195mtrash_fini(void *mem, int size)
196{
197	(void)mtrash_ctor(mem, size, NULL, 0);
198}
199