2 * Copyright (c) 2002, Jeffrey Roberson <jeff@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * uma_dbg.c Debugging features for UMA users
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/types.h>
40 #include <sys/queue.h>
42 #include <sys/mutex.h>
43 #include <sys/malloc.h>
46 #include <vm/vm_object.h>
47 #include <vm/vm_page.h>
49 #include <vm/uma_int.h>
50 #include <vm/uma_dbg.h>
52 static const u_int32_t uma_junk = 0xdeadc0de;
55 * Checks an item to make sure it hasn't been overwritten since freed.
57 * Complies with standard ctor arg/return
61 trash_ctor(void *mem, int size, void *arg)
66 cnt = size / sizeof(uma_junk);
68 for (p = mem; cnt > 0; cnt--, p++)
70 panic("Memory modified after free %p(%d)\n",
75 * Fills an item with predictable garbage
77 * Complies with standard dtor arg/return
81 trash_dtor(void *mem, int size, void *arg)
86 cnt = size / sizeof(uma_junk);
88 for (p = mem; cnt > 0; cnt--, p++)
93 * Fills an item with predictable garbage
95 * Complies with standard init arg/return
99 trash_init(void *mem, int size)
101 trash_dtor(mem, size, NULL);
105 * Checks an item to make sure it hasn't been overwritten since it was freed.
107 * Complies with standard fini arg/return
111 trash_fini(void *mem, int size)
113 trash_ctor(mem, size, NULL);
117 * Checks an item to make sure it hasn't been overwritten since freed.
119 * Complies with standard ctor arg/return
123 mtrash_ctor(void *mem, int size, void *arg)
125 struct malloc_type **ksp;
129 size -= sizeof(struct malloc_type *);
130 ksp = (struct malloc_type **)mem;
131 ksp += size / sizeof(struct malloc_type *);
132 cnt = size / sizeof(uma_junk);
134 for (p = mem; cnt > 0; cnt--, p++)
135 if (*p != uma_junk) {
136 printf("Memory modified after free %p(%d)\n",
138 panic("Most recently used by %s\n", (*ksp == NULL)?
139 "none" : (*ksp)->ks_shortdesc);
144 * Fills an item with predictable garbage
146 * Complies with standard dtor arg/return
150 mtrash_dtor(void *mem, int size, void *arg)
155 size -= sizeof(struct malloc_type *);
156 cnt = size / sizeof(uma_junk);
158 for (p = mem; cnt > 0; cnt--, p++)
163 * Fills an item with predictable garbage
165 * Complies with standard init arg/return
169 mtrash_init(void *mem, int size)
171 struct malloc_type **ksp;
173 mtrash_dtor(mem, size, NULL);
175 ksp = (struct malloc_type **)mem;
176 ksp += (size / sizeof(struct malloc_type *)) - 1;
181 * Checks an item to make sure it hasn't been overwritten since it was freed.
183 * Complies with standard fini arg/return
187 mtrash_fini(void *mem, int size)
189 mtrash_ctor(mem, size, NULL);
193 uma_dbg_getslab(uma_zone_t zone, void *item)
198 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK));
199 if (zone->uz_flags & UMA_ZFLAG_MALLOC) {
200 slab = vtoslab((vm_offset_t)mem);
201 } else if (zone->uz_flags & UMA_ZFLAG_HASH) {
203 slab = hash_sfind(&zone->uz_hash, mem);
206 mem += zone->uz_pgoff;
207 slab = (uma_slab_t)mem;
214 * Set up the slab's freei data such that uma_dbg_free can function.
219 uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
224 slab = uma_dbg_getslab(zone, item);
226 panic("uma: item %p did not belong to zone %s\n",
227 item, zone->uz_name);
230 freei = ((unsigned long)item - (unsigned long)slab->us_data)
233 slab->us_freelist[freei] = 255;
239 * Verifies freed addresses. Checks for alignment, valid slab membership
240 * and duplicate frees.
245 uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
250 slab = uma_dbg_getslab(zone, item);
252 panic("uma: Freed item %p did not belong to zone %s\n",
253 item, zone->uz_name);
256 freei = ((unsigned long)item - (unsigned long)slab->us_data)
259 if (freei >= zone->uz_ipers)
260 panic("zone: %s(%p) slab %p freelist %d out of range 0-%d\n",
261 zone->uz_name, zone, slab, freei, zone->uz_ipers-1);
263 if (((freei * zone->uz_rsize) + slab->us_data) != item) {
264 printf("zone: %s(%p) slab %p freed address %p unaligned.\n",
265 zone->uz_name, zone, slab, item);
266 panic("should be %p\n",
267 (freei * zone->uz_rsize) + slab->us_data);
270 if (slab->us_freelist[freei] != 255) {
271 printf("Slab at %p, freei %d = %d.\n",
272 slab, freei, slab->us_freelist[freei]);
273 panic("Duplicate free of item %p from zone %p(%s)\n",
274 item, zone, zone->uz_name);
278 * When this is actually linked into the slab this will change.
279 * Until then the count of valid slabs will make sure we don't
280 * accidentally follow this and assume it's a valid index.
282 slab->us_freelist[freei] = 0;