kern_malloc.c revision 1.134 1 1.134 para /* $NetBSD: kern_malloc.c,v 1.134 2012/01/27 19:48:40 para Exp $ */
2 1.9 cgd
3 1.1 cgd /*
4 1.8 cgd * Copyright (c) 1987, 1991, 1993
5 1.8 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.81 agc * 3. Neither the name of the University nor the names of its contributors
16 1.81 agc * may be used to endorse or promote products derived from this software
17 1.81 agc * without specific prior written permission.
18 1.81 agc *
19 1.81 agc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.81 agc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.81 agc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.81 agc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.81 agc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.81 agc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.81 agc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.81 agc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.81 agc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.81 agc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.81 agc * SUCH DAMAGE.
30 1.81 agc *
31 1.81 agc * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
32 1.81 agc */
33 1.81 agc
34 1.81 agc /*
35 1.81 agc * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
36 1.81 agc *
37 1.81 agc * Redistribution and use in source and binary forms, with or without
38 1.81 agc * modification, are permitted provided that the following conditions
39 1.81 agc * are met:
40 1.81 agc * 1. Redistributions of source code must retain the above copyright
41 1.81 agc * notice, this list of conditions and the following disclaimer.
42 1.81 agc * 2. Redistributions in binary form must reproduce the above copyright
43 1.81 agc * notice, this list of conditions and the following disclaimer in the
44 1.81 agc * documentation and/or other materials provided with the distribution.
45 1.1 cgd * 3. All advertising materials mentioning features or use of this software
46 1.1 cgd * must display the following acknowledgement:
47 1.1 cgd * This product includes software developed by the University of
48 1.1 cgd * California, Berkeley and its contributors.
49 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
50 1.1 cgd * may be used to endorse or promote products derived from this software
51 1.1 cgd * without specific prior written permission.
52 1.1 cgd *
53 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 1.1 cgd * SUCH DAMAGE.
64 1.1 cgd *
65 1.32 fvdl * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
66 1.1 cgd */
67 1.64 lukem
68 1.64 lukem #include <sys/cdefs.h>
69 1.134 para __KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.134 2012/01/27 19:48:40 para Exp $");
70 1.1 cgd
71 1.7 mycroft #include <sys/param.h>
72 1.7 mycroft #include <sys/proc.h>
73 1.7 mycroft #include <sys/kernel.h>
74 1.7 mycroft #include <sys/malloc.h>
75 1.134 para #include <sys/kmem.h>
76 1.12 christos #include <sys/systm.h>
77 1.106 ad #include <sys/debug.h>
78 1.109 ad #include <sys/mutex.h>
79 1.113 ad #include <sys/lockdebug.h>
80 1.24 thorpej
81 1.28 mrg #include <uvm/uvm_extern.h>
82 1.28 mrg
83 1.24 thorpej #include "opt_kmemstats.h"
84 1.27 thorpej #include "opt_malloclog.h"
85 1.71 fvdl #include "opt_malloc_debug.h"
86 1.12 christos
87 1.99 chs struct kmembuckets kmembuckets[MINBUCKET + 16];
88 1.1 cgd struct kmemusage *kmemusage;
89 1.77 thorpej struct malloc_type *kmemstatistics;
90 1.1 cgd
91 1.134 para kmutex_t malloc_lock;
92 1.27 thorpej
93 1.134 para extern void *kmem_intr_alloc(size_t, km_flag_t);
94 1.134 para extern void *kmem_intr_zalloc(size_t, km_flag_t);
95 1.134 para extern void kmem_intr_free(void *, size_t);
96 1.8 cgd
97 1.134 para struct malloc_header {
98 1.134 para size_t mh_size;
99 1.8 cgd };
100 1.78 pk
101 1.77 thorpej /*
102 1.1 cgd * Allocate a block of memory
103 1.1 cgd */
104 1.27 thorpej #ifdef MALLOCLOG
105 1.27 thorpej void *
106 1.125 pooka _kern_malloc(unsigned long size, struct malloc_type *ksp, int flags,
107 1.77 thorpej const char *file, long line)
108 1.27 thorpej #else
109 1.1 cgd void *
110 1.125 pooka kern_malloc(unsigned long size, struct malloc_type *ksp, int flags)
111 1.27 thorpej #endif /* MALLOCLOG */
112 1.1 cgd {
113 1.134 para struct malloc_header *mh;
114 1.134 para int kmflags = ((flags & M_NOWAIT) != 0
115 1.134 para ? KM_NOSLEEP : KM_SLEEP);
116 1.134 para size_t allocsize = sizeof(struct malloc_header) + size;
117 1.134 para void *p;
118 1.134 para
119 1.134 para p = kmem_intr_alloc(allocsize, kmflags);
120 1.134 para if (p == NULL)
121 1.134 para return NULL;
122 1.1 cgd
123 1.134 para if ((flags & M_ZERO) != 0) {
124 1.134 para memset(p, 0, allocsize);
125 1.8 cgd }
126 1.134 para mh = (void *)p;
127 1.134 para mh->mh_size = allocsize;
128 1.11 cgd
129 1.134 para return mh + 1;
130 1.1 cgd }
131 1.1 cgd
132 1.1 cgd /*
133 1.1 cgd * Free a block of memory allocated by malloc.
134 1.1 cgd */
135 1.27 thorpej #ifdef MALLOCLOG
136 1.27 thorpej void
137 1.125 pooka _kern_free(void *addr, struct malloc_type *ksp, const char *file, long line)
138 1.27 thorpej #else
139 1.1 cgd void
140 1.125 pooka kern_free(void *addr, struct malloc_type *ksp)
141 1.27 thorpej #endif /* MALLOCLOG */
142 1.1 cgd {
143 1.134 para struct malloc_header *mh;
144 1.48 thorpej
145 1.134 para mh = addr;
146 1.134 para mh--;
147 1.62 thorpej
148 1.134 para kmem_intr_free(mh, mh->mh_size);
149 1.20 cgd }
150 1.20 cgd
151 1.20 cgd /*
152 1.20 cgd * Change the size of a block of memory.
153 1.20 cgd */
154 1.20 cgd void *
155 1.126 pooka kern_realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
156 1.77 thorpej int flags)
157 1.20 cgd {
158 1.134 para struct malloc_header *mh;
159 1.72 thorpej unsigned long cursize;
160 1.20 cgd void *newaddr;
161 1.20 cgd
162 1.20 cgd /*
163 1.69 enami * realloc() with a NULL pointer is the same as malloc().
164 1.20 cgd */
165 1.20 cgd if (curaddr == NULL)
166 1.77 thorpej return (malloc(newsize, ksp, flags));
167 1.20 cgd
168 1.20 cgd /*
169 1.69 enami * realloc() with zero size is the same as free().
170 1.20 cgd */
171 1.20 cgd if (newsize == 0) {
172 1.77 thorpej free(curaddr, ksp);
173 1.20 cgd return (NULL);
174 1.20 cgd }
175 1.59 thorpej
176 1.59 thorpej #ifdef LOCKDEBUG
177 1.119 ad if ((flags & M_NOWAIT) == 0) {
178 1.118 yamt ASSERT_SLEEPABLE();
179 1.119 ad }
180 1.59 thorpej #endif
181 1.20 cgd
182 1.134 para mh = curaddr;
183 1.134 para mh--;
184 1.20 cgd
185 1.134 para cursize = mh->mh_size;
186 1.20 cgd
187 1.20 cgd /*
188 1.20 cgd * If we already actually have as much as they want, we're done.
189 1.20 cgd */
190 1.20 cgd if (newsize <= cursize)
191 1.20 cgd return (curaddr);
192 1.20 cgd
193 1.20 cgd /*
194 1.20 cgd * Can't satisfy the allocation with the existing block.
195 1.20 cgd * Allocate a new one and copy the data.
196 1.20 cgd */
197 1.77 thorpej newaddr = malloc(newsize, ksp, flags);
198 1.51 thorpej if (__predict_false(newaddr == NULL)) {
199 1.20 cgd /*
200 1.69 enami * malloc() failed, because flags included M_NOWAIT.
201 1.20 cgd * Return NULL to indicate that failure. The old
202 1.20 cgd * pointer is still valid.
203 1.20 cgd */
204 1.69 enami return (NULL);
205 1.20 cgd }
206 1.34 perry memcpy(newaddr, curaddr, cursize);
207 1.20 cgd
208 1.20 cgd /*
209 1.20 cgd * We were successful: free the old allocation and return
210 1.20 cgd * the new one.
211 1.20 cgd */
212 1.77 thorpej free(curaddr, ksp);
213 1.20 cgd return (newaddr);
214 1.70 enami }
215 1.70 enami
216 1.70 enami /*
217 1.77 thorpej * Add a malloc type to the system.
218 1.77 thorpej */
219 1.77 thorpej void
220 1.77 thorpej malloc_type_attach(struct malloc_type *type)
221 1.77 thorpej {
222 1.77 thorpej
223 1.77 thorpej if (type->ks_magic != M_MAGIC)
224 1.77 thorpej panic("malloc_type_attach: bad magic");
225 1.77 thorpej
226 1.77 thorpej #ifdef DIAGNOSTIC
227 1.77 thorpej {
228 1.77 thorpej struct malloc_type *ksp;
229 1.77 thorpej for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
230 1.77 thorpej if (ksp == type)
231 1.131 christos panic("%s: `%s' already on list", __func__,
232 1.131 christos type->ks_shortdesc);
233 1.77 thorpej }
234 1.77 thorpej }
235 1.77 thorpej #endif
236 1.77 thorpej
237 1.77 thorpej #ifdef KMEMSTATS
238 1.77 thorpej #else
239 1.77 thorpej type->ks_limit = 0;
240 1.77 thorpej #endif
241 1.77 thorpej
242 1.77 thorpej type->ks_next = kmemstatistics;
243 1.77 thorpej kmemstatistics = type;
244 1.77 thorpej }
245 1.77 thorpej
246 1.77 thorpej /*
247 1.77 thorpej * Remove a malloc type from the system..
248 1.77 thorpej */
249 1.77 thorpej void
250 1.77 thorpej malloc_type_detach(struct malloc_type *type)
251 1.77 thorpej {
252 1.77 thorpej struct malloc_type *ksp;
253 1.77 thorpej
254 1.77 thorpej #ifdef DIAGNOSTIC
255 1.77 thorpej if (type->ks_magic != M_MAGIC)
256 1.77 thorpej panic("malloc_type_detach: bad magic");
257 1.77 thorpej #endif
258 1.77 thorpej
259 1.77 thorpej if (type == kmemstatistics)
260 1.77 thorpej kmemstatistics = type->ks_next;
261 1.77 thorpej else {
262 1.77 thorpej for (ksp = kmemstatistics; ksp->ks_next != NULL;
263 1.77 thorpej ksp = ksp->ks_next) {
264 1.77 thorpej if (ksp->ks_next == type) {
265 1.77 thorpej ksp->ks_next = type->ks_next;
266 1.77 thorpej break;
267 1.77 thorpej }
268 1.77 thorpej }
269 1.77 thorpej #ifdef DIAGNOSTIC
270 1.77 thorpej if (ksp->ks_next == NULL)
271 1.77 thorpej panic("malloc_type_detach: not on list");
272 1.77 thorpej #endif
273 1.77 thorpej }
274 1.77 thorpej type->ks_next = NULL;
275 1.77 thorpej }
276 1.77 thorpej
277 1.77 thorpej /*
278 1.77 thorpej * Set the limit on a malloc type.
279 1.77 thorpej */
280 1.77 thorpej void
281 1.105 yamt malloc_type_setlimit(struct malloc_type *type, u_long limit)
282 1.77 thorpej {
283 1.77 thorpej #ifdef KMEMSTATS
284 1.113 ad mutex_spin_enter(&malloc_lock);
285 1.77 thorpej type->ks_limit = limit;
286 1.113 ad mutex_spin_exit(&malloc_lock);
287 1.77 thorpej #endif
288 1.77 thorpej }
289 1.77 thorpej
290 1.77 thorpej /*
291 1.1 cgd * Initialize the kernel memory allocator
292 1.1 cgd */
293 1.12 christos void
294 1.69 enami kmeminit(void)
295 1.1 cgd {
296 1.77 thorpej __link_set_decl(malloc_types, struct malloc_type);
297 1.77 thorpej struct malloc_type * const *ksp;
298 1.23 tls #ifdef KMEMSTATS
299 1.50 augustss long indx;
300 1.23 tls #endif
301 1.1 cgd
302 1.116 ad mutex_init(&malloc_lock, MUTEX_DEFAULT, IPL_VM);
303 1.109 ad
304 1.1 cgd #ifdef KMEMSTATS
305 1.1 cgd for (indx = 0; indx < MINBUCKET + 16; indx++) {
306 1.49 thorpej if (1 << indx >= PAGE_SIZE)
307 1.99 chs kmembuckets[indx].kb_elmpercl = 1;
308 1.1 cgd else
309 1.99 chs kmembuckets[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
310 1.99 chs kmembuckets[indx].kb_highwat =
311 1.99 chs 5 * kmembuckets[indx].kb_elmpercl;
312 1.1 cgd }
313 1.62 thorpej #endif
314 1.77 thorpej
315 1.77 thorpej /* Attach all of the statically-linked malloc types. */
316 1.77 thorpej __link_set_foreach(ksp, malloc_types)
317 1.77 thorpej malloc_type_attach(*ksp);
318 1.127 pooka
319 1.127 pooka #ifdef MALLOC_DEBUG
320 1.127 pooka debug_malloc_init();
321 1.127 pooka #endif
322 1.1 cgd }
323 1.39 thorpej
324 1.39 thorpej #ifdef DDB
325 1.39 thorpej #include <ddb/db_output.h>
326 1.39 thorpej
327 1.39 thorpej /*
328 1.39 thorpej * Dump kmem statistics from ddb.
329 1.39 thorpej *
330 1.39 thorpej * usage: call dump_kmemstats
331 1.39 thorpej */
332 1.69 enami void dump_kmemstats(void);
333 1.39 thorpej
334 1.39 thorpej void
335 1.69 enami dump_kmemstats(void)
336 1.39 thorpej {
337 1.39 thorpej #ifdef KMEMSTATS
338 1.77 thorpej struct malloc_type *ksp;
339 1.39 thorpej
340 1.77 thorpej for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
341 1.77 thorpej if (ksp->ks_memuse == 0)
342 1.77 thorpej continue;
343 1.77 thorpej db_printf("%s%.*s %ld\n", ksp->ks_shortdesc,
344 1.77 thorpej (int)(20 - strlen(ksp->ks_shortdesc)),
345 1.77 thorpej " ",
346 1.77 thorpej ksp->ks_memuse);
347 1.39 thorpej }
348 1.39 thorpej #else
349 1.39 thorpej db_printf("Kmem stats are not being collected.\n");
350 1.39 thorpej #endif /* KMEMSTATS */
351 1.39 thorpej }
352 1.39 thorpej #endif /* DDB */
353 1.82 manu
354 1.82 manu
355 1.82 manu #if 0
356 1.96 perry /*
357 1.82 manu * Diagnostic messages about "Data modified on
358 1.82 manu * freelist" indicate a memory corruption, but
359 1.82 manu * they do not help tracking it down.
360 1.96 perry * This function can be called at various places
361 1.82 manu * to sanity check malloc's freelist and discover
362 1.82 manu * where does the corruption take place.
363 1.82 manu */
364 1.82 manu int
365 1.82 manu freelist_sanitycheck(void) {
366 1.82 manu int i,j;
367 1.82 manu struct kmembuckets *kbp;
368 1.82 manu struct freelist *freep;
369 1.82 manu int rv = 0;
370 1.96 perry
371 1.82 manu for (i = MINBUCKET; i <= MINBUCKET + 15; i++) {
372 1.99 chs kbp = &kmembuckets[i];
373 1.82 manu freep = (struct freelist *)kbp->kb_next;
374 1.82 manu j = 0;
375 1.82 manu while(freep) {
376 1.82 manu vm_map_lock(kmem_map);
377 1.82 manu rv = uvm_map_checkprot(kmem_map, (vaddr_t)freep,
378 1.96 perry (vaddr_t)freep + sizeof(struct freelist),
379 1.82 manu VM_PROT_WRITE);
380 1.82 manu vm_map_unlock(kmem_map);
381 1.82 manu
382 1.82 manu if ((rv == 0) || (*(int *)freep != WEIRD_ADDR)) {
383 1.82 manu printf("bucket %i, chunck %d at %p modified\n",
384 1.82 manu i, j, freep);
385 1.82 manu return 1;
386 1.82 manu }
387 1.82 manu freep = (struct freelist *)freep->next;
388 1.82 manu j++;
389 1.82 manu }
390 1.82 manu }
391 1.82 manu
392 1.82 manu return 0;
393 1.82 manu }
394 1.82 manu #endif
395