2 * Copyright (c) 2002, 2003, 2004, 2005 Jeffrey Roberson <jeff@FreeBSD.org>
3 * Copyright (c) 2004, 2005 Bosko Milekic <bmilekic@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
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.
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.
29 * uma_dbg.c Debugging features for UMA users
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
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>
43 #include <sys/mutex.h>
44 #include <sys/malloc.h>
47 #include <vm/vm_object.h>
48 #include <vm/vm_page.h>
50 #include <vm/uma_int.h>
51 #include <vm/uma_dbg.h>
53 static const uint32_t uma_junk = 0xdeadc0de;
56 * Checks an item to make sure it hasn't been overwritten since it was freed,
57 * prior to subsequent reallocation.
59 * Complies with standard ctor arg/return
63 trash_ctor(void *mem, int size, void *arg, int flags)
68 cnt = size / sizeof(uma_junk);
70 for (p = mem; cnt > 0; cnt--, p++)
72 printf("Memory modified after free %p(%d) val=%x @ %p\n",
80 * Fills an item with predictable garbage
82 * Complies with standard dtor arg/return
86 trash_dtor(void *mem, int size, void *arg)
91 cnt = size / sizeof(uma_junk);
93 for (p = mem; cnt > 0; cnt--, p++)
98 * Fills an item with predictable garbage
100 * Complies with standard init arg/return
104 trash_init(void *mem, int size, int flags)
106 trash_dtor(mem, size, NULL);
111 * Checks an item to make sure it hasn't been overwritten since it was freed.
113 * Complies with standard fini arg/return
117 trash_fini(void *mem, int size)
119 (void)trash_ctor(mem, size, NULL, 0);
123 mtrash_ctor(void *mem, int size, void *arg, int flags)
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) val=%x @ %p\n",
138 panic("Most recently used by %s\n", (*ksp == NULL)?
139 "none" : (*ksp)->ks_shortdesc);
145 * Fills an item with predictable garbage
147 * Complies with standard dtor arg/return
151 mtrash_dtor(void *mem, int size, void *arg)
156 size -= sizeof(struct malloc_type *);
157 cnt = size / sizeof(uma_junk);
159 for (p = mem; cnt > 0; cnt--, p++)
164 * Fills an item with predictable garbage
166 * Complies with standard init arg/return
170 mtrash_init(void *mem, int size, int flags)
172 struct malloc_type **ksp;
174 mtrash_dtor(mem, size, NULL);
176 ksp = (struct malloc_type **)mem;
177 ksp += (size / sizeof(struct malloc_type *)) - 1;
183 * Checks an item to make sure it hasn't been overwritten since it was freed,
184 * prior to freeing it back to available memory.
186 * Complies with standard fini arg/return
190 mtrash_fini(void *mem, int size)
192 (void)mtrash_ctor(mem, size, NULL, 0);
197 uma_dbg_getslab(uma_zone_t zone, void *item)
203 mem = (uint8_t *)((uintptr_t)item & (~UMA_SLAB_MASK));
204 if (zone->uz_flags & UMA_ZONE_VTOSLAB) {
205 slab = vtoslab((vm_offset_t)mem);
208 * It is safe to return the slab here even though the
209 * zone is unlocked because the item's allocation state
210 * essentially holds a reference.
213 keg = LIST_FIRST(&zone->uz_kegs)->kl_keg;
214 if (keg->uk_flags & UMA_ZONE_HASH)
215 slab = hash_sfind(&keg->uk_hash, mem);
217 slab = (uma_slab_t)(mem + keg->uk_pgoff);
225 * Set up the slab's freei data such that uma_dbg_free can function.
229 uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
234 if (zone_first_keg(zone) == NULL)
237 slab = uma_dbg_getslab(zone, item);
239 panic("uma: item %p did not belong to zone %s\n",
240 item, zone->uz_name);
243 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
245 if (BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
246 panic("Duplicate alloc of %p from zone %p(%s) slab %p(%d)\n",
247 item, zone, zone->uz_name, slab, freei);
248 BIT_SET_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
254 * Verifies freed addresses. Checks for alignment, valid slab membership
255 * and duplicate frees.
259 uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
264 if (zone_first_keg(zone) == NULL)
267 slab = uma_dbg_getslab(zone, item);
269 panic("uma: Freed item %p did not belong to zone %s\n",
270 item, zone->uz_name);
273 freei = ((uintptr_t)item - (uintptr_t)slab->us_data) / keg->uk_rsize;
275 if (freei >= keg->uk_ipers)
276 panic("Invalid free of %p from zone %p(%s) slab %p(%d)\n",
277 item, zone, zone->uz_name, slab, freei);
279 if (((freei * keg->uk_rsize) + slab->us_data) != item)
280 panic("Unaligned free of %p from zone %p(%s) slab %p(%d)\n",
281 item, zone, zone->uz_name, slab, freei);
283 if (!BIT_ISSET(SLAB_SETSIZE, freei, &slab->us_debugfree))
284 panic("Duplicate free of %p from zone %p(%s) slab %p(%d)\n",
285 item, zone, zone->uz_name, slab, freei);
287 BIT_CLR_ATOMIC(SLAB_SETSIZE, freei, &slab->us_debugfree);
290 #endif /* INVARIANTS */