uma_dbg.c revision 147615
1/*- 2 * Copyright (c) 2004, 2005, 3 * Bosko Milekic <bmilekic@FreeBSD.org>. All rights reserved. 4 * Copyright (c) 2002, 2003, 2004, 2005, 5 * Jeffrey Roberson <jeff@FreeBSD.org>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29/* 30 * uma_dbg.c Debugging features for UMA users 31 * 32 */ 33 34#include <sys/cdefs.h> 35__FBSDID("$FreeBSD: head/sys/vm/uma_dbg.c 147615 2005-06-26 23:44:07Z silby $"); 36 37#include <sys/param.h> 38#include <sys/systm.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 u_int32_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 u_int32_t *p; 67 68 cnt = size / sizeof(uma_junk); 69 70 for (p = mem; cnt > 0; cnt--, p++) 71 if (*p != uma_junk) { 72 printf("Memory modified after free %p(%d) val=%x @ %p\n", 73 mem, size, *p, p); 74 return (0); 75 } 76 return (0); 77} 78 79/* 80 * Fills an item with predictable garbage 81 * 82 * Complies with standard dtor arg/return 83 * 84 */ 85void 86trash_dtor(void *mem, int size, void *arg) 87{ 88 int cnt; 89 u_int32_t *p; 90 91 cnt = size / sizeof(uma_junk); 92 93 for (p = mem; cnt > 0; cnt--, p++) 94 *p = uma_junk; 95} 96 97/* 98 * Fills an item with predictable garbage 99 * 100 * Complies with standard init arg/return 101 * 102 */ 103int 104trash_init(void *mem, int size, int flags) 105{ 106 trash_dtor(mem, size, NULL); 107 return (0); 108} 109 110/* 111 * Checks an item to make sure it hasn't been overwritten since it was freed. 112 * 113 * Complies with standard fini arg/return 114 * 115 */ 116void 117trash_fini(void *mem, int size) 118{ 119 (void)trash_ctor(mem, size, NULL, 0); 120} 121 122int 123mtrash_ctor(void *mem, int size, void *arg, int flags) 124{ 125 struct malloc_type **ksp; 126 u_int32_t *p = mem; 127 int cnt; 128 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); 133 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", 137 mem, size, *p, p); 138 panic("Most recently used by %s\n", (*ksp == NULL)? 139 "none" : (*ksp)->ks_shortdesc); 140 } 141 return (0); 142} 143 144/* 145 * Fills an item with predictable garbage 146 * 147 * Complies with standard dtor arg/return 148 * 149 */ 150void 151mtrash_dtor(void *mem, int size, void *arg) 152{ 153 int cnt; 154 u_int32_t *p; 155 156 size -= sizeof(struct malloc_type *); 157 cnt = size / sizeof(uma_junk); 158 159 for (p = mem; cnt > 0; cnt--, p++) 160 *p = uma_junk; 161} 162 163/* 164 * Fills an item with predictable garbage 165 * 166 * Complies with standard init arg/return 167 * 168 */ 169int 170mtrash_init(void *mem, int size, int flags) 171{ 172 struct malloc_type **ksp; 173 174 mtrash_dtor(mem, size, NULL); 175 176 ksp = (struct malloc_type **)mem; 177 ksp += (size / sizeof(struct malloc_type *)) - 1; 178 *ksp = NULL; 179 return (0); 180} 181 182/* 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. 185 * 186 * Complies with standard fini arg/return 187 * 188 */ 189void 190mtrash_fini(void *mem, int size) 191{ 192 (void)mtrash_ctor(mem, size, NULL, 0); 193} 194 195static uma_slab_t 196uma_dbg_getslab(uma_zone_t zone, void *item) 197{ 198 uma_slab_t slab; 199 uma_keg_t keg; 200 u_int8_t *mem; 201 202 keg = zone->uz_keg; 203 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK)); 204 if (keg->uk_flags & UMA_ZONE_MALLOC) { 205 slab = vtoslab((vm_offset_t)mem); 206 } else if (keg->uk_flags & UMA_ZONE_HASH) { 207 slab = hash_sfind(&keg->uk_hash, mem); 208 } else { 209 mem += keg->uk_pgoff; 210 slab = (uma_slab_t)mem; 211 } 212 213 return (slab); 214} 215 216/* 217 * Set up the slab's freei data such that uma_dbg_free can function. 218 * 219 */ 220 221void 222uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 223{ 224 uma_keg_t keg; 225 uma_slabrefcnt_t slabref; 226 int freei; 227 228 keg = zone->uz_keg; 229 if (slab == NULL) { 230 slab = uma_dbg_getslab(zone, item); 231 if (slab == NULL) 232 panic("uma: item %p did not belong to zone %s\n", 233 item, zone->uz_name); 234 } 235 236 freei = ((unsigned long)item - (unsigned long)slab->us_data) 237 / keg->uk_rsize; 238 239 if (keg->uk_flags & UMA_ZONE_REFCNT) { 240 slabref = (uma_slabrefcnt_t)slab; 241 slabref->us_freelist[freei].us_item = 255; 242 } else { 243 slab->us_freelist[freei].us_item = 255; 244 } 245 246 return; 247} 248 249/* 250 * Verifies freed addresses. Checks for alignment, valid slab membership 251 * and duplicate frees. 252 * 253 */ 254 255void 256uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 257{ 258 uma_keg_t keg; 259 uma_slabrefcnt_t slabref; 260 int freei; 261 262 keg = zone->uz_keg; 263 if (slab == NULL) { 264 slab = uma_dbg_getslab(zone, item); 265 if (slab == NULL) 266 panic("uma: Freed item %p did not belong to zone %s\n", 267 item, zone->uz_name); 268 } 269 270 freei = ((unsigned long)item - (unsigned long)slab->us_data) 271 / keg->uk_rsize; 272 273 if (freei >= keg->uk_ipers) 274 panic("zone: %s(%p) slab %p freelist %d out of range 0-%d\n", 275 zone->uz_name, zone, slab, freei, keg->uk_ipers-1); 276 277 if (((freei * keg->uk_rsize) + slab->us_data) != item) { 278 printf("zone: %s(%p) slab %p freed address %p unaligned.\n", 279 zone->uz_name, zone, slab, item); 280 panic("should be %p\n", 281 (freei * keg->uk_rsize) + slab->us_data); 282 } 283 284 if (keg->uk_flags & UMA_ZONE_REFCNT) { 285 slabref = (uma_slabrefcnt_t)slab; 286 if (slabref->us_freelist[freei].us_item != 255) { 287 printf("Slab at %p, freei %d = %d.\n", 288 slab, freei, slabref->us_freelist[freei].us_item); 289 panic("Duplicate free of item %p from zone %p(%s)\n", 290 item, zone, zone->uz_name); 291 } 292 293 /* 294 * When this is actually linked into the slab this will change. 295 * Until then the count of valid slabs will make sure we don't 296 * accidentally follow this and assume it's a valid index. 297 */ 298 slabref->us_freelist[freei].us_item = 0; 299 } else { 300 if (slab->us_freelist[freei].us_item != 255) { 301 printf("Slab at %p, freei %d = %d.\n", 302 slab, freei, slab->us_freelist[freei].us_item); 303 panic("Duplicate free of item %p from zone %p(%s)\n", 304 item, zone, zone->uz_name); 305 } 306 307 /* 308 * When this is actually linked into the slab this will change. 309 * Until then the count of valid slabs will make sure we don't 310 * accidentally follow this and assume it's a valid index. 311 */ 312 slab->us_freelist[freei].us_item = 0; 313 } 314} 315