uma_dbg.c revision 95899
1/* 2 * Copyright (c) 2002, Jeffrey Roberson <jroberson@chesapeake.net> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 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. 14 * 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. 25 * 26 * $FreeBSD: head/sys/vm/uma_dbg.c 95899 2002-05-02 02:08:48Z jeff $ 27 * 28 */ 29 30/* 31 * uma_dbg.c Debugging features for UMA users 32 * 33 */ 34 35 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> 41#include <sys/lock.h> 42#include <sys/mutex.h> 43 44#include <machine/types.h> 45 46#include <vm/uma.h> 47#include <vm/uma_int.h> 48#include <vm/uma_dbg.h> 49 50static const u_int32_t uma_junk = 0xdeadc0de; 51 52/* 53 * Checks an item to make sure it hasn't been overwritten since freed. 54 * 55 * Complies with standard ctor arg/return 56 * 57 */ 58void 59trash_ctor(void *mem, int size, void *arg) 60{ 61 int cnt; 62 u_int32_t *p; 63 64 cnt = size / sizeof(uma_junk); 65 66 for (p = mem; cnt > 0; cnt--, p++) 67 if (*p != uma_junk) 68 panic("Memory modified after free %p(%d)\n", 69 mem, size); 70} 71 72/* 73 * Fills an item with predictable garbage 74 * 75 * Complies with standard dtor arg/return 76 * 77 */ 78void 79trash_dtor(void *mem, int size, void *arg) 80{ 81 int cnt; 82 u_int32_t *p; 83 84 cnt = size / sizeof(uma_junk); 85 86 for (p = mem; cnt > 0; cnt--, p++) 87 *p = uma_junk; 88} 89 90/* 91 * Fills an item with predictable garbage 92 * 93 * Complies with standard init arg/return 94 * 95 */ 96void 97trash_init(void *mem, int size) 98{ 99 trash_dtor(mem, size, NULL); 100} 101 102/* 103 * Checks an item to make sure it hasn't been overwritten since it was freed. 104 * 105 * Complies with standard fini arg/return 106 * 107 */ 108void 109trash_fini(void *mem, int size) 110{ 111 trash_ctor(mem, size, NULL); 112} 113 114static uma_slab_t 115uma_dbg_getslab(uma_zone_t zone, void *item) 116{ 117 uma_slab_t slab; 118 u_int8_t *mem; 119 120 mem = (u_int8_t *)((unsigned long)item & (~UMA_SLAB_MASK)); 121 if (zone->uz_flags & UMA_ZFLAG_MALLOC) { 122 slab = hash_sfind(mallochash, mem); 123 } else if (zone->uz_flags & UMA_ZFLAG_OFFPAGE) { 124 ZONE_LOCK(zone); 125 slab = hash_sfind(&zone->uz_hash, mem); 126 ZONE_UNLOCK(zone); 127 } else { 128 mem += zone->uz_pgoff; 129 slab = (uma_slab_t)mem; 130 } 131 132 return (slab); 133} 134 135/* 136 * Set up the slab's freei data such that uma_dbg_free can function. 137 * 138 */ 139 140void 141uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item) 142{ 143 int freei; 144 145 if (slab == NULL) { 146 slab = uma_dbg_getslab(zone, item); 147 if (slab == NULL) 148 panic("uma: item %p did not belong to zone %s\n", 149 item, zone->uz_name); 150 } 151 152 freei = ((unsigned long)item - (unsigned long)slab->us_data) 153 / zone->uz_rsize; 154 155 slab->us_freelist[freei] = 255; 156 157 return; 158} 159 160/* 161 * Verifies freed addresses. Checks for alignment, valid slab membership 162 * and duplicate frees. 163 * 164 */ 165 166void 167uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item) 168{ 169 int freei; 170 171 return; 172 173 if (slab == NULL) { 174 slab = uma_dbg_getslab(zone, item); 175 if (slab == NULL) 176 panic("uma: Freed item %p did not belong to zone %s\n", 177 item, zone->uz_name); 178 } 179 180 freei = ((unsigned long)item - (unsigned long)slab->us_data) 181 / zone->uz_rsize; 182 183 if (freei >= zone->uz_ipers) 184 panic("zone: %s(%p) slab %p freelist %i out of range 0-%d\n", 185 zone->uz_name, zone, slab, freei, zone->uz_ipers-1); 186 187 if (((freei * zone->uz_rsize) + slab->us_data) != item) { 188 printf("zone: %s(%p) slab %p freed address %p unaligned.\n", 189 zone->uz_name, zone, slab, item); 190 panic("should be %p\n", 191 (freei * zone->uz_rsize) + slab->us_data); 192 } 193 194 if (slab->us_freelist[freei] != 255) { 195 printf("Slab at %p, freei %d = %d.\n", 196 slab, freei, slab->us_freelist[freei]); 197 panic("Duplicate free of item %p from zone %p(%s)\n", 198 item, zone, zone->uz_name); 199 } 200 201 /* 202 * When this is actually linked into the slab this will change. 203 * Until then the count of valid slabs will make sure we don't 204 * accidentally follow this and assume it's a valid index. 205 */ 206 slab->us_freelist[freei] = 0; 207} 208