uma_dbg.c revision 295222
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 295222 2016-02-03 23:30:17Z 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#include <sys/taskqueue.h>
46
47#include <vm/vm.h>
48#include <vm/vm_object.h>
49#include <vm/vm_page.h>
50#include <vm/uma.h>
51#include <vm/uma_int.h>
52#include <vm/uma_dbg.h>
53
54static const uint32_t uma_junk = 0xdeadc0de;
55
56/*
57 * Checks an item to make sure it hasn't been overwritten since it was freed,
58 * prior to subsequent reallocation.
59 *
60 * Complies with standard ctor arg/return
61 *
62 */
63int
64trash_ctor(void *mem, int size, void *arg, int flags)
65{
66	int cnt;
67	uint32_t *p;
68
69	cnt = size / sizeof(uma_junk);
70
71	for (p = mem; cnt > 0; cnt--, p++)
72		if (*p != uma_junk) {
73#ifdef INVARIANTS
74			panic("Memory modified after free %p(%d) val=%x @ %p\n",
75			    mem, size, *p, p);
76#else
77			printf("Memory modified after free %p(%d) val=%x @ %p\n",
78			    mem, size, *p, p);
79#endif
80			return (0);
81		}
82	return (0);
83}
84
85/*
86 * Fills an item with predictable garbage
87 *
88 * Complies with standard dtor arg/return
89 *
90 */
91void
92trash_dtor(void *mem, int size, void *arg)
93{
94	int cnt;
95	uint32_t *p;
96
97	cnt = size / sizeof(uma_junk);
98
99	for (p = mem; cnt > 0; cnt--, p++)
100		*p = uma_junk;
101}
102
103/*
104 * Fills an item with predictable garbage
105 *
106 * Complies with standard init arg/return
107 *
108 */
109int
110trash_init(void *mem, int size, int flags)
111{
112	trash_dtor(mem, size, NULL);
113	return (0);
114}
115
116/*
117 * Checks an item to make sure it hasn't been overwritten since it was freed.
118 *
119 * Complies with standard fini arg/return
120 *
121 */
122void
123trash_fini(void *mem, int size)
124{
125	(void)trash_ctor(mem, size, NULL, 0);
126}
127
128int
129mtrash_ctor(void *mem, int size, void *arg, int flags)
130{
131	struct malloc_type **ksp;
132	uint32_t *p = mem;
133	int cnt;
134
135	size -= sizeof(struct malloc_type *);
136	ksp = (struct malloc_type **)mem;
137	ksp += size / sizeof(struct malloc_type *);
138	cnt = size / sizeof(uma_junk);
139
140	for (p = mem; cnt > 0; cnt--, p++)
141		if (*p != uma_junk) {
142			printf("Memory modified after free %p(%d) val=%x @ %p\n",
143			    mem, size, *p, p);
144			panic("Most recently used by %s\n", (*ksp == NULL)?
145			    "none" : (*ksp)->ks_shortdesc);
146		}
147	return (0);
148}
149
150/*
151 * Fills an item with predictable garbage
152 *
153 * Complies with standard dtor arg/return
154 *
155 */
156void
157mtrash_dtor(void *mem, int size, void *arg)
158{
159	int cnt;
160	uint32_t *p;
161
162	size -= sizeof(struct malloc_type *);
163	cnt = size / sizeof(uma_junk);
164
165	for (p = mem; cnt > 0; cnt--, p++)
166		*p = uma_junk;
167}
168
169/*
170 * Fills an item with predictable garbage
171 *
172 * Complies with standard init arg/return
173 *
174 */
175int
176mtrash_init(void *mem, int size, int flags)
177{
178	struct malloc_type **ksp;
179
180	mtrash_dtor(mem, size, NULL);
181
182	ksp = (struct malloc_type **)mem;
183	ksp += (size / sizeof(struct malloc_type *)) - 1;
184	*ksp = NULL;
185	return (0);
186}
187
188/*
189 * Checks an item to make sure it hasn't been overwritten since it was freed,
190 * prior to freeing it back to available memory.
191 *
192 * Complies with standard fini arg/return
193 *
194 */
195void
196mtrash_fini(void *mem, int size)
197{
198	(void)mtrash_ctor(mem, size, NULL, 0);
199}
200