kern_malloc.c revision 34266
1/*
2 * Copyright (c) 1987, 1991, 1993
3 *	The Regents of the University of California.  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, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *	@(#)kern_malloc.c	8.3 (Berkeley) 1/4/94
34 * $Id: kern_malloc.c,v 1.44 1998/02/23 07:41:23 dyson Exp $
35 */
36
37#include "opt_vm.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#define MALLOC_INSTANTIATE
43#include <sys/malloc.h>
44#include <sys/mbuf.h>
45#include <sys/vmmeter.h>
46#include <sys/lock.h>
47
48#include <vm/vm.h>
49#include <vm/vm_param.h>
50#include <vm/vm_kern.h>
51#include <vm/vm_extern.h>
52#include <vm/pmap.h>
53#include <vm/vm_map.h>
54
55static void kmeminit __P((void *));
56static void malloc_init __P((struct malloc_type *));
57SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL)
58
59static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
60
61static struct malloc_type *kmemstatistics = M_FREE;
62static struct kmembuckets bucket[MINBUCKET + 16];
63static struct kmemusage *kmemusage;
64static char *kmembase;
65static char *kmemlimit;
66static int vm_kmem_size;
67
68#ifdef DIAGNOSTIC
69/*
70 * This structure provides a set of masks to catch unaligned frees.
71 */
72static long addrmask[] = { 0,
73	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
74	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
75	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
76	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
77};
78
79/*
80 * The WEIRD_ADDR is used as known text to copy into free objects so
81 * that modifications after frees can be detected.
82 */
83#define WEIRD_ADDR	0xdeadc0de
84#define MAX_COPY	64
85
86/*
87 * Normally the first word of the structure is used to hold the list
88 * pointer for free objects. However, when running with diagnostics,
89 * we use the third and fourth fields, so as to catch modifications
90 * in the most commonly trashed first two words.
91 */
92struct freelist {
93	long	spare0;
94	struct malloc_type *type;
95	long	spare1;
96	caddr_t	next;
97};
98#else /* !DIAGNOSTIC */
99struct freelist {
100	caddr_t	next;
101};
102#endif /* DIAGNOSTIC */
103
104/*
105 * Allocate a block of memory
106 */
107void *
108malloc(size, type, flags)
109	unsigned long size;
110	struct malloc_type *type;
111	int flags;
112{
113	register struct kmembuckets *kbp;
114	register struct kmemusage *kup;
115	register struct freelist *freep;
116	long indx, npg, allocsize;
117	int s;
118	caddr_t va, cp, savedlist;
119#ifdef DIAGNOSTIC
120	long *end, *lp;
121	int copysize;
122	char *savedtype;
123#endif
124	register struct malloc_type *ksp = type;
125
126	if (!type->ks_next)
127		malloc_init(type);
128
129	indx = BUCKETINDX(size);
130	kbp = &bucket[indx];
131	s = splmem();
132	while (ksp->ks_memuse >= ksp->ks_limit) {
133		if (flags & M_NOWAIT) {
134			splx(s);
135			return ((void *) NULL);
136		}
137		if (ksp->ks_limblocks < 65535)
138			ksp->ks_limblocks++;
139		tsleep((caddr_t)ksp, PSWP+2, type->ks_shortdesc, 0);
140	}
141	ksp->ks_size |= 1 << indx;
142#ifdef DIAGNOSTIC
143	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
144#endif
145	if (kbp->kb_next == NULL) {
146		kbp->kb_last = NULL;
147		if (size > MAXALLOCSAVE)
148			allocsize = roundup(size, PAGE_SIZE);
149		else
150			allocsize = 1 << indx;
151		npg = btoc(allocsize);
152		va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg), flags);
153		if (va == NULL) {
154			splx(s);
155			return ((void *) NULL);
156		}
157		kbp->kb_total += kbp->kb_elmpercl;
158		kup = btokup(va);
159		kup->ku_indx = indx;
160		if (allocsize > MAXALLOCSAVE) {
161			if (npg > 65535)
162				panic("malloc: allocation too large");
163			kup->ku_pagecnt = npg;
164			ksp->ks_memuse += allocsize;
165			goto out;
166		}
167		kup->ku_freecnt = kbp->kb_elmpercl;
168		kbp->kb_totalfree += kbp->kb_elmpercl;
169		/*
170		 * Just in case we blocked while allocating memory,
171		 * and someone else also allocated memory for this
172		 * bucket, don't assume the list is still empty.
173		 */
174		savedlist = kbp->kb_next;
175		kbp->kb_next = cp = va + (npg * PAGE_SIZE) - allocsize;
176		for (;;) {
177			freep = (struct freelist *)cp;
178#ifdef DIAGNOSTIC
179			/*
180			 * Copy in known text to detect modification
181			 * after freeing.
182			 */
183			end = (long *)&cp[copysize];
184			for (lp = (long *)cp; lp < end; lp++)
185				*lp = WEIRD_ADDR;
186			freep->type = M_FREE;
187#endif /* DIAGNOSTIC */
188			if (cp <= va)
189				break;
190			cp -= allocsize;
191			freep->next = cp;
192		}
193		freep->next = savedlist;
194		if (kbp->kb_last == NULL)
195			kbp->kb_last = (caddr_t)freep;
196	}
197	va = kbp->kb_next;
198	kbp->kb_next = ((struct freelist *)va)->next;
199#ifdef DIAGNOSTIC
200	freep = (struct freelist *)va;
201	savedtype = (char *) type->ks_shortdesc;
202#if BYTE_ORDER == BIG_ENDIAN
203	freep->type = (struct malloc_type *)WEIRD_ADDR >> 16;
204#endif
205#if BYTE_ORDER == LITTLE_ENDIAN
206	freep->type = (struct malloc_type *)WEIRD_ADDR;
207#endif
208	if (((long)(&freep->next)) & 0x2)
209		freep->next = (caddr_t)((WEIRD_ADDR >> 16)|(WEIRD_ADDR << 16));
210	else
211		freep->next = (caddr_t)WEIRD_ADDR;
212	end = (long *)&va[copysize];
213	for (lp = (long *)va; lp < end; lp++) {
214		if (*lp == WEIRD_ADDR)
215			continue;
216		printf("%s %d of object %p size %ld %s %s (0x%lx != 0x%x)\n",
217			"Data modified on freelist: word", lp - (long *)va,
218			va, size, "previous type", savedtype, *lp, WEIRD_ADDR);
219		break;
220	}
221	freep->spare0 = 0;
222#endif /* DIAGNOSTIC */
223	kup = btokup(va);
224	if (kup->ku_indx != indx)
225		panic("malloc: wrong bucket");
226	if (kup->ku_freecnt == 0)
227		panic("malloc: lost data");
228	kup->ku_freecnt--;
229	kbp->kb_totalfree--;
230	ksp->ks_memuse += 1 << indx;
231out:
232	kbp->kb_calls++;
233	ksp->ks_inuse++;
234	ksp->ks_calls++;
235	if (ksp->ks_memuse > ksp->ks_maxused)
236		ksp->ks_maxused = ksp->ks_memuse;
237	splx(s);
238	return ((void *) va);
239}
240
241/*
242 * Free a block of memory allocated by malloc.
243 */
244void
245free(addr, type)
246	void *addr;
247	struct malloc_type *type;
248{
249	register struct kmembuckets *kbp;
250	register struct kmemusage *kup;
251	register struct freelist *freep;
252	long size;
253	int s;
254#ifdef DIAGNOSTIC
255	struct freelist *fp;
256	long *end, *lp, alloc, copysize;
257#endif
258	register struct malloc_type *ksp = type;
259
260	if (!type->ks_next)
261		panic("freeing with unknown type (%s)", type->ks_shortdesc);
262
263#ifdef DIAGNOSTIC
264	if ((char *)addr < kmembase || (char *)addr >= kmemlimit) {
265		panic("free: address 0x%x out of range", addr);
266	}
267#endif
268	kup = btokup(addr);
269	size = 1 << kup->ku_indx;
270	kbp = &bucket[kup->ku_indx];
271	s = splmem();
272#ifdef DIAGNOSTIC
273	/*
274	 * Check for returns of data that do not point to the
275	 * beginning of the allocation.
276	 */
277	if (size > PAGE_SIZE)
278		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
279	else
280		alloc = addrmask[kup->ku_indx];
281	if (((u_long)addr & alloc) != 0)
282		panic("free: unaligned addr 0x%x, size %d, type %s, mask %d",
283			addr, size, type->ks_shortdesc, alloc);
284#endif /* DIAGNOSTIC */
285	if (size > MAXALLOCSAVE) {
286		kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
287		size = kup->ku_pagecnt << PAGE_SHIFT;
288		ksp->ks_memuse -= size;
289		kup->ku_indx = 0;
290		kup->ku_pagecnt = 0;
291		if (ksp->ks_memuse + size >= ksp->ks_limit &&
292		    ksp->ks_memuse < ksp->ks_limit)
293			wakeup((caddr_t)ksp);
294		ksp->ks_inuse--;
295		kbp->kb_total -= 1;
296		splx(s);
297		return;
298	}
299	freep = (struct freelist *)addr;
300#ifdef DIAGNOSTIC
301	/*
302	 * Check for multiple frees. Use a quick check to see if
303	 * it looks free before laboriously searching the freelist.
304	 */
305	if (freep->spare0 == WEIRD_ADDR) {
306		fp = (struct freelist *)kbp->kb_next;
307		while (fp) {
308			if (fp->spare0 != WEIRD_ADDR) {
309				printf("trashed free item %p\n", fp);
310				panic("free: free item modified");
311			} else if (addr == (caddr_t)fp) {
312				printf("multiple freed item %p\n", addr);
313				panic("free: multiple free");
314			}
315			fp = (struct freelist *)fp->next;
316		}
317	}
318	/*
319	 * Copy in known text to detect modification after freeing
320	 * and to make it look free. Also, save the type being freed
321	 * so we can list likely culprit if modification is detected
322	 * when the object is reallocated.
323	 */
324	copysize = size < MAX_COPY ? size : MAX_COPY;
325	end = (long *)&((caddr_t)addr)[copysize];
326	for (lp = (long *)addr; lp < end; lp++)
327		*lp = WEIRD_ADDR;
328	freep->type = type;
329#endif /* DIAGNOSTIC */
330	kup->ku_freecnt++;
331	if (kup->ku_freecnt >= kbp->kb_elmpercl)
332		if (kup->ku_freecnt > kbp->kb_elmpercl)
333			panic("free: multiple frees");
334		else if (kbp->kb_totalfree > kbp->kb_highwat)
335			kbp->kb_couldfree++;
336	kbp->kb_totalfree++;
337	ksp->ks_memuse -= size;
338	if (ksp->ks_memuse + size >= ksp->ks_limit &&
339	    ksp->ks_memuse < ksp->ks_limit)
340		wakeup((caddr_t)ksp);
341	ksp->ks_inuse--;
342#ifdef OLD_MALLOC_MEMORY_POLICY
343	if (kbp->kb_next == NULL)
344		kbp->kb_next = addr;
345	else
346		((struct freelist *)kbp->kb_last)->next = addr;
347	freep->next = NULL;
348	kbp->kb_last = addr;
349#else
350	/*
351	 * Return memory to the head of the queue for quick reuse.  This
352	 * can improve performance by improving the probability of the
353	 * item being in the cache when it is reused.
354	 */
355	if (kbp->kb_next == NULL) {
356		kbp->kb_next = addr;
357		kbp->kb_last = addr;
358		freep->next = NULL;
359	} else {
360		freep->next = kbp->kb_next;
361		kbp->kb_next = addr;
362	}
363#endif
364	splx(s);
365}
366
367/*
368 * Initialize the kernel memory allocator
369 */
370/* ARGSUSED*/
371static void
372kmeminit(dummy)
373	void *dummy;
374{
375	register long indx;
376	int npg;
377	int mem_size;
378
379#if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
380#error "kmeminit: MAXALLOCSAVE not power of 2"
381#endif
382#if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
383#error "kmeminit: MAXALLOCSAVE too big"
384#endif
385#if	(MAXALLOCSAVE < PAGE_SIZE)
386#error "kmeminit: MAXALLOCSAVE too small"
387#endif
388
389	/*
390	 * Try to auto-tune the kernel memory size, so that it is
391	 * more applicable for a wider range of machine sizes.
392	 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
393	 * a VM_KMEM_SIZE of 12MB is a fair compromise.  The
394	 * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
395	 * available, and on an X86 with a total KVA space of 256MB,
396	 * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
397	 *
398	 * Note that the kmem_map is also used by the zone allocator,
399	 * so make sure that there is enough space.
400	 */
401	vm_kmem_size = VM_KMEM_SIZE;
402	mem_size = cnt.v_page_count * PAGE_SIZE;
403
404#if defined(VM_KMEM_SIZE_SCALE)
405	if ((mem_size / VM_KMEM_SIZE_SCALE) > vm_kmem_size)
406		vm_kmem_size = mem_size / VM_KMEM_SIZE_SCALE;
407#endif
408
409#if defined(VM_KMEM_SIZE_MAX)
410	if (vm_kmem_size >= VM_KMEM_SIZE_MAX)
411		vm_kmem_size = VM_KMEM_SIZE_MAX;
412#endif
413
414	if (vm_kmem_size > 2 * (cnt.v_page_count * PAGE_SIZE))
415		vm_kmem_size = 2 * (cnt.v_page_count * PAGE_SIZE);
416
417	npg = (nmbufs * MSIZE + nmbclusters * MCLBYTES + vm_kmem_size)
418		/ PAGE_SIZE;
419
420	kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
421		(vm_size_t)(npg * sizeof(struct kmemusage)));
422	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
423		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE));
424	kmem_map->system_map = 1;
425	for (indx = 0; indx < MINBUCKET + 16; indx++) {
426		if (1 << indx >= PAGE_SIZE)
427			bucket[indx].kb_elmpercl = 1;
428		else
429			bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
430		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
431	}
432}
433
434static void
435malloc_init(type)
436	struct malloc_type *type;
437{
438	int npg;
439	int mem_size;
440
441	if (type->ks_magic != M_MAGIC)
442		panic("malloc type lacks magic");
443
444	if (cnt.v_page_count == 0)
445		panic("malloc_init not allowed before vm init");
446
447	/*
448	 * The default limits for each malloc region is 1/2 of the
449	 * malloc portion of the kmem map size.
450	 */
451	type->ks_limit = vm_kmem_size / 2;
452	type->ks_next = kmemstatistics;
453	kmemstatistics = type;
454}
455