uma_dbg.c revision 95923
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 95923 2002-05-02 07:22:19Z 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		mtx_lock(&malloc_mtx);
123		slab = hash_sfind(mallochash, mem);
124		mtx_unlock(&malloc_mtx);
125	} else if (zone->uz_flags & UMA_ZFLAG_OFFPAGE) {
126		ZONE_LOCK(zone);
127		slab = hash_sfind(&zone->uz_hash, mem);
128		ZONE_UNLOCK(zone);
129	} else {
130		mem += zone->uz_pgoff;
131		slab = (uma_slab_t)mem;
132	}
133
134	return (slab);
135}
136
137/*
138 * Set up the slab's freei data such that uma_dbg_free can function.
139 *
140 */
141
142void
143uma_dbg_alloc(uma_zone_t zone, uma_slab_t slab, void *item)
144{
145	int freei;
146
147	if (slab == NULL) {
148		slab = uma_dbg_getslab(zone, item);
149		if (slab == NULL)
150			panic("uma: item %p did not belong to zone %s\n",
151			    item, zone->uz_name);
152	}
153
154	freei = ((unsigned long)item - (unsigned long)slab->us_data)
155	    / zone->uz_rsize;
156
157	slab->us_freelist[freei] = 255;
158
159	return;
160}
161
162/*
163 * Verifies freed addresses.  Checks for alignment, valid slab membership
164 * and duplicate frees.
165 *
166 */
167
168void
169uma_dbg_free(uma_zone_t zone, uma_slab_t slab, void *item)
170{
171	int freei;
172
173	return;
174
175	if (slab == NULL) {
176		slab = uma_dbg_getslab(zone, item);
177		if (slab == NULL)
178			panic("uma: Freed item %p did not belong to zone %s\n",
179			    item, zone->uz_name);
180	}
181
182	freei = ((unsigned long)item - (unsigned long)slab->us_data)
183	    / zone->uz_rsize;
184
185	if (freei >= zone->uz_ipers)
186		panic("zone: %s(%p) slab %p freelist %i out of range 0-%d\n",
187		    zone->uz_name, zone, slab, freei, zone->uz_ipers-1);
188
189	if (((freei * zone->uz_rsize) + slab->us_data) != item) {
190		printf("zone: %s(%p) slab %p freed address %p unaligned.\n",
191		    zone->uz_name, zone, slab, item);
192		panic("should be %p\n",
193		    (freei * zone->uz_rsize) + slab->us_data);
194	}
195
196	if (slab->us_freelist[freei] != 255) {
197		printf("Slab at %p, freei %d = %d.\n",
198		    slab, freei, slab->us_freelist[freei]);
199		panic("Duplicate free of item %p from zone %p(%s)\n",
200		    item, zone, zone->uz_name);
201	}
202
203	/*
204	 * When this is actually linked into the slab this will change.
205	 * Until then the count of valid slabs will make sure we don't
206	 * accidentally follow this and assume it's a valid index.
207	 */
208	slab->us_freelist[freei] = 0;
209}
210