xref: /dflybsd-src/sys/kern/kern_slaballoc.c (revision d2a4c6204a77aaec984b85e9754b5aa783192476)
1 /*
2  * KERN_SLABALLOC.C	- Kernel SLAB memory allocator (MP SAFE)
3  *
4  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
5  *
6  * This code is derived from software contributed to The DragonFly Project
7  * by Matthew Dillon <dillon@backplane.com>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  * 3. Neither the name of The DragonFly Project nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific, prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
27  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * $DragonFly: src/sys/kern/kern_slaballoc.c,v 1.31 2005/04/26 00:47:59 dillon Exp $
37  *
38  * This module implements a slab allocator drop-in replacement for the
39  * kernel malloc().
40  *
41  * A slab allocator reserves a ZONE for each chunk size, then lays the
42  * chunks out in an array within the zone.  Allocation and deallocation
43  * is nearly instantanious, and fragmentation/overhead losses are limited
44  * to a fixed worst-case amount.
45  *
46  * The downside of this slab implementation is in the chunk size
47  * multiplied by the number of zones.  ~80 zones * 128K = 10MB of VM per cpu.
48  * In a kernel implementation all this memory will be physical so
49  * the zone size is adjusted downward on machines with less physical
50  * memory.  The upside is that overhead is bounded... this is the *worst*
51  * case overhead.
52  *
53  * Slab management is done on a per-cpu basis and no locking or mutexes
54  * are required, only a critical section.  When one cpu frees memory
55  * belonging to another cpu's slab manager an asynchronous IPI message
56  * will be queued to execute the operation.   In addition, both the
57  * high level slab allocator and the low level zone allocator optimize
58  * M_ZERO requests, and the slab allocator does not have to pre initialize
59  * the linked list of chunks.
60  *
61  * XXX Balancing is needed between cpus.  Balance will be handled through
62  * asynchronous IPIs primarily by reassigning the z_Cpu ownership of chunks.
63  *
64  * XXX If we have to allocate a new zone and M_USE_RESERVE is set, use of
65  * the new zone should be restricted to M_USE_RESERVE requests only.
66  *
67  *	Alloc Size	Chunking        Number of zones
68  *	0-127		8		16
69  *	128-255		16		8
70  *	256-511		32		8
71  *	512-1023	64		8
72  *	1024-2047	128		8
73  *	2048-4095	256		8
74  *	4096-8191	512		8
75  *	8192-16383	1024		8
76  *	16384-32767	2048		8
77  *	(if PAGE_SIZE is 4K the maximum zone allocation is 16383)
78  *
79  *	Allocations >= ZoneLimit go directly to kmem.
80  *
81  *			API REQUIREMENTS AND SIDE EFFECTS
82  *
83  *    To operate as a drop-in replacement to the FreeBSD-4.x malloc() we
84  *    have remained compatible with the following API requirements:
85  *
86  *    + small power-of-2 sized allocations are power-of-2 aligned (kern_tty)
87  *    + all power-of-2 sized allocations are power-of-2 aligned (twe)
88  *    + malloc(0) is allowed and returns non-NULL (ahc driver)
89  *    + ability to allocate arbitrarily large chunks of memory
90  */
91 
92 #include "opt_vm.h"
93 
94 #include <sys/param.h>
95 #include <sys/systm.h>
96 #include <sys/kernel.h>
97 #include <sys/slaballoc.h>
98 #include <sys/mbuf.h>
99 #include <sys/vmmeter.h>
100 #include <sys/lock.h>
101 #include <sys/thread.h>
102 #include <sys/globaldata.h>
103 
104 #include <vm/vm.h>
105 #include <vm/vm_param.h>
106 #include <vm/vm_kern.h>
107 #include <vm/vm_extern.h>
108 #include <vm/vm_object.h>
109 #include <vm/pmap.h>
110 #include <vm/vm_map.h>
111 #include <vm/vm_page.h>
112 #include <vm/vm_pageout.h>
113 
114 #include <machine/cpu.h>
115 
116 #include <sys/thread2.h>
117 
118 #define arysize(ary)	(sizeof(ary)/sizeof((ary)[0]))
119 
120 /*
121  * Fixed globals (not per-cpu)
122  */
123 static int ZoneSize;
124 static int ZoneLimit;
125 static int ZonePageCount;
126 static int ZoneMask;
127 static struct malloc_type *kmemstatistics;
128 static struct kmemusage *kmemusage;
129 static int32_t weirdary[16];
130 
131 static void *kmem_slab_alloc(vm_size_t bytes, vm_offset_t align, int flags);
132 static void kmem_slab_free(void *ptr, vm_size_t bytes);
133 
134 /*
135  * Misc constants.  Note that allocations that are exact multiples of
136  * PAGE_SIZE, or exceed the zone limit, fall through to the kmem module.
137  * IN_SAME_PAGE_MASK is used to sanity-check the per-page free lists.
138  */
139 #define MIN_CHUNK_SIZE		8		/* in bytes */
140 #define MIN_CHUNK_MASK		(MIN_CHUNK_SIZE - 1)
141 #define ZONE_RELS_THRESH	2		/* threshold number of zones */
142 #define IN_SAME_PAGE_MASK	(~(intptr_t)PAGE_MASK | MIN_CHUNK_MASK)
143 
144 /*
145  * The WEIRD_ADDR is used as known text to copy into free objects to
146  * try to create deterministic failure cases if the data is accessed after
147  * free.
148  */
149 #define WEIRD_ADDR      0xdeadc0de
150 #define MAX_COPY        sizeof(weirdary)
151 #define ZERO_LENGTH_PTR	((void *)-8)
152 
153 /*
154  * Misc global malloc buckets
155  */
156 
157 MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
158 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
159 MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
160 
161 MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
162 MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
163 
164 /*
165  * Initialize the slab memory allocator.  We have to choose a zone size based
166  * on available physical memory.  We choose a zone side which is approximately
167  * 1/1024th of our memory, so if we have 128MB of ram we have a zone size of
168  * 128K.  The zone size is limited to the bounds set in slaballoc.h
169  * (typically 32K min, 128K max).
170  */
171 static void kmeminit(void *dummy);
172 
173 SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL)
174 
175 static void
176 kmeminit(void *dummy)
177 {
178     vm_poff_t limsize;
179     int usesize;
180     int i;
181     vm_pindex_t npg;
182 
183     limsize = (vm_poff_t)vmstats.v_page_count * PAGE_SIZE;
184     if (limsize > VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS)
185 	limsize = VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS;
186 
187     usesize = (int)(limsize / 1024);	/* convert to KB */
188 
189     ZoneSize = ZALLOC_MIN_ZONE_SIZE;
190     while (ZoneSize < ZALLOC_MAX_ZONE_SIZE && (ZoneSize << 1) < usesize)
191 	ZoneSize <<= 1;
192     ZoneLimit = ZoneSize / 4;
193     if (ZoneLimit > ZALLOC_ZONE_LIMIT)
194 	ZoneLimit = ZALLOC_ZONE_LIMIT;
195     ZoneMask = ZoneSize - 1;
196     ZonePageCount = ZoneSize / PAGE_SIZE;
197 
198     npg = (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE;
199     kmemusage = kmem_slab_alloc(npg * sizeof(struct kmemusage), PAGE_SIZE, M_WAITOK|M_ZERO);
200 
201     for (i = 0; i < arysize(weirdary); ++i)
202 	weirdary[i] = WEIRD_ADDR;
203 
204     if (bootverbose)
205 	printf("Slab ZoneSize set to %dKB\n", ZoneSize / 1024);
206 }
207 
208 /*
209  * Initialize a malloc type tracking structure.
210  */
211 void
212 malloc_init(void *data)
213 {
214     struct malloc_type *type = data;
215     vm_poff_t limsize;
216 
217     if (type->ks_magic != M_MAGIC)
218 	panic("malloc type lacks magic");
219 
220     if (type->ks_limit != 0)
221 	return;
222 
223     if (vmstats.v_page_count == 0)
224 	panic("malloc_init not allowed before vm init");
225 
226     limsize = (vm_poff_t)vmstats.v_page_count * PAGE_SIZE;
227     if (limsize > VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS)
228 	limsize = VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS;
229     type->ks_limit = limsize / 10;
230 
231     type->ks_next = kmemstatistics;
232     kmemstatistics = type;
233 }
234 
235 void
236 malloc_uninit(void *data)
237 {
238     struct malloc_type *type = data;
239     struct malloc_type *t;
240 #ifdef INVARIANTS
241     int i;
242     long ttl;
243 #endif
244 
245     if (type->ks_magic != M_MAGIC)
246 	panic("malloc type lacks magic");
247 
248     if (vmstats.v_page_count == 0)
249 	panic("malloc_uninit not allowed before vm init");
250 
251     if (type->ks_limit == 0)
252 	panic("malloc_uninit on uninitialized type");
253 
254 #ifdef INVARIANTS
255     /*
256      * memuse is only correct in aggregation.  Due to memory being allocated
257      * on one cpu and freed on another individual array entries may be
258      * negative or positive (canceling each other out).
259      */
260     for (i = ttl = 0; i < ncpus; ++i)
261 	ttl += type->ks_memuse[i];
262     if (ttl) {
263 	printf("malloc_uninit: %ld bytes of '%s' still allocated on cpu %d\n",
264 	    ttl, type->ks_shortdesc, i);
265     }
266 #endif
267     if (type == kmemstatistics) {
268 	kmemstatistics = type->ks_next;
269     } else {
270 	for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) {
271 	    if (t->ks_next == type) {
272 		t->ks_next = type->ks_next;
273 		break;
274 	    }
275 	}
276     }
277     type->ks_next = NULL;
278     type->ks_limit = 0;
279 }
280 
281 /*
282  * Calculate the zone index for the allocation request size and set the
283  * allocation request size to that particular zone's chunk size.
284  */
285 static __inline int
286 zoneindex(unsigned long *bytes)
287 {
288     unsigned int n = (unsigned int)*bytes;	/* unsigned for shift opt */
289     if (n < 128) {
290 	*bytes = n = (n + 7) & ~7;
291 	return(n / 8 - 1);		/* 8 byte chunks, 16 zones */
292     }
293     if (n < 256) {
294 	*bytes = n = (n + 15) & ~15;
295 	return(n / 16 + 7);
296     }
297     if (n < 8192) {
298 	if (n < 512) {
299 	    *bytes = n = (n + 31) & ~31;
300 	    return(n / 32 + 15);
301 	}
302 	if (n < 1024) {
303 	    *bytes = n = (n + 63) & ~63;
304 	    return(n / 64 + 23);
305 	}
306 	if (n < 2048) {
307 	    *bytes = n = (n + 127) & ~127;
308 	    return(n / 128 + 31);
309 	}
310 	if (n < 4096) {
311 	    *bytes = n = (n + 255) & ~255;
312 	    return(n / 256 + 39);
313 	}
314 	*bytes = n = (n + 511) & ~511;
315 	return(n / 512 + 47);
316     }
317 #if ZALLOC_ZONE_LIMIT > 8192
318     if (n < 16384) {
319 	*bytes = n = (n + 1023) & ~1023;
320 	return(n / 1024 + 55);
321     }
322 #endif
323 #if ZALLOC_ZONE_LIMIT > 16384
324     if (n < 32768) {
325 	*bytes = n = (n + 2047) & ~2047;
326 	return(n / 2048 + 63);
327     }
328 #endif
329     panic("Unexpected byte count %d", n);
330     return(0);
331 }
332 
333 /*
334  * malloc()	(SLAB ALLOCATOR) (MP SAFE)
335  *
336  *	Allocate memory via the slab allocator.  If the request is too large,
337  *	or if it page-aligned beyond a certain size, we fall back to the
338  *	KMEM subsystem.  A SLAB tracking descriptor must be specified, use
339  *	&SlabMisc if you don't care.
340  *
341  *	M_RNOWAIT	- don't block.
342  *	M_NULLOK	- return NULL instead of blocking.
343  *	M_ZERO		- zero the returned memory.
344  *	M_USE_RESERVE	- allow greater drawdown of the free list
345  *	M_USE_INTERRUPT_RESERVE - allow the freelist to be exhausted
346  */
347 void *
348 malloc(unsigned long size, struct malloc_type *type, int flags)
349 {
350     SLZone *z;
351     SLChunk *chunk;
352     SLGlobalData *slgd;
353     struct globaldata *gd;
354     int zi;
355 
356     gd = mycpu;
357     slgd = &gd->gd_slab;
358 
359     /*
360      * XXX silly to have this in the critical path.
361      */
362     if (type->ks_limit == 0) {
363 	crit_enter();
364 	if (type->ks_limit == 0)
365 	    malloc_init(type);
366 	crit_exit();
367     }
368     ++type->ks_calls;
369 
370     /*
371      * Handle the case where the limit is reached.  Panic if we can't return
372      * NULL.  The original malloc code looped, but this tended to
373      * simply deadlock the computer.
374      *
375      * ks_loosememuse is an up-only limit that is NOT MP-synchronized, used
376      * to determine if a more complete limit check should be done.  The
377      * actual memory use is tracked via ks_memuse[cpu].
378      */
379     while (type->ks_loosememuse >= type->ks_limit) {
380 	int i;
381 	long ttl;
382 
383 	for (i = ttl = 0; i < ncpus; ++i)
384 	    ttl += type->ks_memuse[i];
385 	type->ks_loosememuse = ttl;	/* not MP synchronized */
386 	if (ttl >= type->ks_limit) {
387 	    if (flags & M_NULLOK)
388 		return(NULL);
389 	    panic("%s: malloc limit exceeded", type->ks_shortdesc);
390 	}
391     }
392 
393     /*
394      * Handle the degenerate size == 0 case.  Yes, this does happen.
395      * Return a special pointer.  This is to maintain compatibility with
396      * the original malloc implementation.  Certain devices, such as the
397      * adaptec driver, not only allocate 0 bytes, they check for NULL and
398      * also realloc() later on.  Joy.
399      */
400     if (size == 0)
401 	return(ZERO_LENGTH_PTR);
402 
403     /*
404      * Handle hysteresis from prior frees here in malloc().  We cannot
405      * safely manipulate the kernel_map in free() due to free() possibly
406      * being called via an IPI message or from sensitive interrupt code.
407      */
408     while (slgd->NFreeZones > ZONE_RELS_THRESH && (flags & M_RNOWAIT) == 0) {
409 	crit_enter();
410 	if (slgd->NFreeZones > ZONE_RELS_THRESH) {	/* crit sect race */
411 	    z = slgd->FreeZones;
412 	    slgd->FreeZones = z->z_Next;
413 	    --slgd->NFreeZones;
414 	    kmem_slab_free(z, ZoneSize);	/* may block */
415 	}
416 	crit_exit();
417     }
418     /*
419      * XXX handle oversized frees that were queued from free().
420      */
421     while (slgd->FreeOvZones && (flags & M_RNOWAIT) == 0) {
422 	crit_enter();
423 	if ((z = slgd->FreeOvZones) != NULL) {
424 	    KKASSERT(z->z_Magic == ZALLOC_OVSZ_MAGIC);
425 	    slgd->FreeOvZones = z->z_Next;
426 	    kmem_slab_free(z, z->z_ChunkSize);	/* may block */
427 	}
428 	crit_exit();
429     }
430 
431     /*
432      * Handle large allocations directly.  There should not be very many of
433      * these so performance is not a big issue.
434      *
435      * Guarentee page alignment for allocations in multiples of PAGE_SIZE
436      */
437     if (size >= ZoneLimit || (size & PAGE_MASK) == 0) {
438 	struct kmemusage *kup;
439 
440 	size = round_page(size);
441 	chunk = kmem_slab_alloc(size, PAGE_SIZE, flags);
442 	if (chunk == NULL)
443 	    return(NULL);
444 	flags &= ~M_ZERO;	/* result already zero'd if M_ZERO was set */
445 	flags |= M_PASSIVE_ZERO;
446 	kup = btokup(chunk);
447 	kup->ku_pagecnt = size / PAGE_SIZE;
448 	kup->ku_cpu = gd->gd_cpuid;
449 	crit_enter();
450 	goto done;
451     }
452 
453     /*
454      * Attempt to allocate out of an existing zone.  First try the free list,
455      * then allocate out of unallocated space.  If we find a good zone move
456      * it to the head of the list so later allocations find it quickly
457      * (we might have thousands of zones in the list).
458      *
459      * Note: zoneindex() will panic of size is too large.
460      */
461     zi = zoneindex(&size);
462     KKASSERT(zi < NZONES);
463     crit_enter();
464     if ((z = slgd->ZoneAry[zi]) != NULL) {
465 	KKASSERT(z->z_NFree > 0);
466 
467 	/*
468 	 * Remove us from the ZoneAry[] when we become empty
469 	 */
470 	if (--z->z_NFree == 0) {
471 	    slgd->ZoneAry[zi] = z->z_Next;
472 	    z->z_Next = NULL;
473 	}
474 
475 	/*
476 	 * Locate a chunk in a free page.  This attempts to localize
477 	 * reallocations into earlier pages without us having to sort
478 	 * the chunk list.  A chunk may still overlap a page boundary.
479 	 */
480 	while (z->z_FirstFreePg < ZonePageCount) {
481 	    if ((chunk = z->z_PageAry[z->z_FirstFreePg]) != NULL) {
482 #ifdef DIAGNOSTIC
483 		/*
484 		 * Diagnostic: c_Next is not total garbage.
485 		 */
486 		KKASSERT(chunk->c_Next == NULL ||
487 			((intptr_t)chunk->c_Next & IN_SAME_PAGE_MASK) ==
488 			((intptr_t)chunk & IN_SAME_PAGE_MASK));
489 #endif
490 #ifdef INVARIANTS
491 		if ((uintptr_t)chunk < VM_MIN_KERNEL_ADDRESS)
492 			panic("chunk %p FFPG %d/%d", chunk, z->z_FirstFreePg, ZonePageCount);
493 		if (chunk->c_Next && (uintptr_t)chunk->c_Next < VM_MIN_KERNEL_ADDRESS)
494 			panic("chunkNEXT %p %p FFPG %d/%d", chunk, chunk->c_Next, z->z_FirstFreePg, ZonePageCount);
495 #endif
496 		z->z_PageAry[z->z_FirstFreePg] = chunk->c_Next;
497 		goto done;
498 	    }
499 	    ++z->z_FirstFreePg;
500 	}
501 
502 	/*
503 	 * No chunks are available but NFree said we had some memory, so
504 	 * it must be available in the never-before-used-memory area
505 	 * governed by UIndex.  The consequences are very serious if our zone
506 	 * got corrupted so we use an explicit panic rather then a KASSERT.
507 	 */
508 	if (z->z_UIndex + 1 != z->z_NMax)
509 	    z->z_UIndex = z->z_UIndex + 1;
510 	else
511 	    z->z_UIndex = 0;
512 	if (z->z_UIndex == z->z_UEndIndex)
513 	    panic("slaballoc: corrupted zone");
514 	chunk = (SLChunk *)(z->z_BasePtr + z->z_UIndex * size);
515 	if ((z->z_Flags & SLZF_UNOTZEROD) == 0) {
516 	    flags &= ~M_ZERO;
517 	    flags |= M_PASSIVE_ZERO;
518 	}
519 	goto done;
520     }
521 
522     /*
523      * If all zones are exhausted we need to allocate a new zone for this
524      * index.  Use M_ZERO to take advantage of pre-zerod pages.  Also see
525      * UAlloc use above in regards to M_ZERO.  Note that when we are reusing
526      * a zone from the FreeZones list UAlloc'd data will not be zero'd, and
527      * we do not pre-zero it because we do not want to mess up the L1 cache.
528      *
529      * At least one subsystem, the tty code (see CROUND) expects power-of-2
530      * allocations to be power-of-2 aligned.  We maintain compatibility by
531      * adjusting the base offset below.
532      */
533     {
534 	int off;
535 
536 	if ((z = slgd->FreeZones) != NULL) {
537 	    slgd->FreeZones = z->z_Next;
538 	    --slgd->NFreeZones;
539 	    bzero(z, sizeof(SLZone));
540 	    z->z_Flags |= SLZF_UNOTZEROD;
541 	} else {
542 	    z = kmem_slab_alloc(ZoneSize, ZoneSize, flags|M_ZERO);
543 	    if (z == NULL)
544 		goto fail;
545 	}
546 
547 	/*
548 	 * Guarentee power-of-2 alignment for power-of-2-sized chunks.
549 	 * Otherwise just 8-byte align the data.
550 	 */
551 	if ((size | (size - 1)) + 1 == (size << 1))
552 	    off = (sizeof(SLZone) + size - 1) & ~(size - 1);
553 	else
554 	    off = (sizeof(SLZone) + MIN_CHUNK_MASK) & ~MIN_CHUNK_MASK;
555 	z->z_Magic = ZALLOC_SLAB_MAGIC;
556 	z->z_ZoneIndex = zi;
557 	z->z_NMax = (ZoneSize - off) / size;
558 	z->z_NFree = z->z_NMax - 1;
559 	z->z_BasePtr = (char *)z + off;
560 	z->z_UIndex = z->z_UEndIndex = slgd->JunkIndex % z->z_NMax;
561 	z->z_ChunkSize = size;
562 	z->z_FirstFreePg = ZonePageCount;
563 	z->z_CpuGd = gd;
564 	z->z_Cpu = gd->gd_cpuid;
565 	chunk = (SLChunk *)(z->z_BasePtr + z->z_UIndex * size);
566 	z->z_Next = slgd->ZoneAry[zi];
567 	slgd->ZoneAry[zi] = z;
568 	if ((z->z_Flags & SLZF_UNOTZEROD) == 0) {
569 	    flags &= ~M_ZERO;	/* already zero'd */
570 	    flags |= M_PASSIVE_ZERO;
571 	}
572 
573 	/*
574 	 * Slide the base index for initial allocations out of the next
575 	 * zone we create so we do not over-weight the lower part of the
576 	 * cpu memory caches.
577 	 */
578 	slgd->JunkIndex = (slgd->JunkIndex + ZALLOC_SLAB_SLIDE)
579 				& (ZALLOC_MAX_ZONE_SIZE - 1);
580     }
581 done:
582     ++type->ks_inuse[gd->gd_cpuid];
583     type->ks_memuse[gd->gd_cpuid] += size;
584     type->ks_loosememuse += size;	/* not MP synchronized */
585     crit_exit();
586     if (flags & M_ZERO)
587 	bzero(chunk, size);
588 #ifdef INVARIANTS
589     else if ((flags & (M_ZERO|M_PASSIVE_ZERO)) == 0)
590 	chunk->c_Next = (void *)-1; /* avoid accidental double-free check */
591 #endif
592     return(chunk);
593 fail:
594     crit_exit();
595     return(NULL);
596 }
597 
598 /*
599  * kernel realloc.  (SLAB ALLOCATOR) (MP SAFE)
600  *
601  * Generally speaking this routine is not called very often and we do
602  * not attempt to optimize it beyond reusing the same pointer if the
603  * new size fits within the chunking of the old pointer's zone.
604  */
605 void *
606 realloc(void *ptr, unsigned long size, struct malloc_type *type, int flags)
607 {
608     SLZone *z;
609     void *nptr;
610     unsigned long osize;
611 
612     KKASSERT((flags & M_ZERO) == 0);	/* not supported */
613 
614     if (ptr == NULL || ptr == ZERO_LENGTH_PTR)
615 	return(malloc(size, type, flags));
616     if (size == 0) {
617 	free(ptr, type);
618 	return(NULL);
619     }
620 
621     /*
622      * Handle oversized allocations.  XXX we really should require that a
623      * size be passed to free() instead of this nonsense.
624      */
625     {
626 	struct kmemusage *kup;
627 
628 	kup = btokup(ptr);
629 	if (kup->ku_pagecnt) {
630 	    osize = kup->ku_pagecnt << PAGE_SHIFT;
631 	    if (osize == round_page(size))
632 		return(ptr);
633 	    if ((nptr = malloc(size, type, flags)) == NULL)
634 		return(NULL);
635 	    bcopy(ptr, nptr, min(size, osize));
636 	    free(ptr, type);
637 	    return(nptr);
638 	}
639     }
640 
641     /*
642      * Get the original allocation's zone.  If the new request winds up
643      * using the same chunk size we do not have to do anything.
644      */
645     z = (SLZone *)((uintptr_t)ptr & ~(uintptr_t)ZoneMask);
646     KKASSERT(z->z_Magic == ZALLOC_SLAB_MAGIC);
647 
648     zoneindex(&size);
649     if (z->z_ChunkSize == size)
650 	return(ptr);
651 
652     /*
653      * Allocate memory for the new request size.  Note that zoneindex has
654      * already adjusted the request size to the appropriate chunk size, which
655      * should optimize our bcopy().  Then copy and return the new pointer.
656      */
657     if ((nptr = malloc(size, type, flags)) == NULL)
658 	return(NULL);
659     bcopy(ptr, nptr, min(size, z->z_ChunkSize));
660     free(ptr, type);
661     return(nptr);
662 }
663 
664 /*
665  * Allocate a copy of the specified string.
666  *
667  * (MP SAFE) (MAY BLOCK)
668  */
669 char *
670 strdup(const char *str, struct malloc_type *type)
671 {
672     int zlen;	/* length inclusive of terminating NUL */
673     char *nstr;
674 
675     if (str == NULL)
676 	return(NULL);
677     zlen = strlen(str) + 1;
678     nstr = malloc(zlen, type, M_WAITOK);
679     bcopy(str, nstr, zlen);
680     return(nstr);
681 }
682 
683 #ifdef SMP
684 /*
685  * free()	(SLAB ALLOCATOR)
686  *
687  *	Free the specified chunk of memory.
688  */
689 static
690 void
691 free_remote(void *ptr)
692 {
693     free(ptr, *(struct malloc_type **)ptr);
694 }
695 
696 #endif
697 
698 /*
699  * free (SLAB ALLOCATOR) (MP SAFE)
700  *
701  * Free a memory block previously allocated by malloc.  Note that we do not
702  * attempt to uplodate ks_loosememuse as MP races could prevent us from
703  * checking memory limits in malloc.
704  */
705 void
706 free(void *ptr, struct malloc_type *type)
707 {
708     SLZone *z;
709     SLChunk *chunk;
710     SLGlobalData *slgd;
711     struct globaldata *gd;
712     int pgno;
713 
714     gd = mycpu;
715     slgd = &gd->gd_slab;
716 
717     if (ptr == NULL)
718 	panic("trying to free NULL pointer");
719 
720     /*
721      * Handle special 0-byte allocations
722      */
723     if (ptr == ZERO_LENGTH_PTR)
724 	return;
725 
726     /*
727      * Handle oversized allocations.  XXX we really should require that a
728      * size be passed to free() instead of this nonsense.
729      *
730      * This code is never called via an ipi.
731      */
732     {
733 	struct kmemusage *kup;
734 	unsigned long size;
735 
736 	kup = btokup(ptr);
737 	if (kup->ku_pagecnt) {
738 	    size = kup->ku_pagecnt << PAGE_SHIFT;
739 	    kup->ku_pagecnt = 0;
740 #ifdef INVARIANTS
741 	    KKASSERT(sizeof(weirdary) <= size);
742 	    bcopy(weirdary, ptr, sizeof(weirdary));
743 #endif
744 	    /*
745 	     * note: we always adjust our cpu's slot, not the originating
746 	     * cpu (kup->ku_cpuid).  The statistics are in aggregate.
747 	     *
748 	     * note: XXX we have still inherited the interrupts-can't-block
749 	     * assumption.  An interrupt thread does not bump
750 	     * gd_intr_nesting_level so check TDF_INTTHREAD.  This is
751 	     * primarily until we can fix softupdate's assumptions about free().
752 	     */
753 	    crit_enter();
754 	    --type->ks_inuse[gd->gd_cpuid];
755 	    type->ks_memuse[gd->gd_cpuid] -= size;
756 	    if (mycpu->gd_intr_nesting_level || (gd->gd_curthread->td_flags & TDF_INTTHREAD)) {
757 		z = (SLZone *)ptr;
758 		z->z_Magic = ZALLOC_OVSZ_MAGIC;
759 		z->z_Next = slgd->FreeOvZones;
760 		z->z_ChunkSize = size;
761 		slgd->FreeOvZones = z;
762 		crit_exit();
763 	    } else {
764 		crit_exit();
765 		kmem_slab_free(ptr, size);	/* may block */
766 	    }
767 	    return;
768 	}
769     }
770 
771     /*
772      * Zone case.  Figure out the zone based on the fact that it is
773      * ZoneSize aligned.
774      */
775     z = (SLZone *)((uintptr_t)ptr & ~(uintptr_t)ZoneMask);
776     KKASSERT(z->z_Magic == ZALLOC_SLAB_MAGIC);
777 
778     /*
779      * If we do not own the zone then forward the request to the
780      * cpu that does.  Since the timing is non-critical, a passive
781      * message is sent.
782      */
783     if (z->z_CpuGd != gd) {
784 	*(struct malloc_type **)ptr = type;
785 #ifdef SMP
786 	lwkt_send_ipiq_passive(z->z_CpuGd, free_remote, ptr);
787 #else
788 	panic("Corrupt SLZone");
789 #endif
790 	return;
791     }
792 
793     if (type->ks_magic != M_MAGIC)
794 	panic("free: malloc type lacks magic");
795 
796     crit_enter();
797     pgno = ((char *)ptr - (char *)z) >> PAGE_SHIFT;
798     chunk = ptr;
799 
800 #ifdef INVARIANTS
801     /*
802      * Attempt to detect a double-free.  To reduce overhead we only check
803      * if there appears to be link pointer at the base of the data.
804      */
805     if (((intptr_t)chunk->c_Next - (intptr_t)z) >> PAGE_SHIFT == pgno) {
806 	SLChunk *scan;
807 	for (scan = z->z_PageAry[pgno]; scan; scan = scan->c_Next) {
808 	    if (scan == chunk)
809 		panic("Double free at %p", chunk);
810 	}
811     }
812 #endif
813 
814     /*
815      * Put weird data into the memory to detect modifications after freeing,
816      * illegal pointer use after freeing (we should fault on the odd address),
817      * and so forth.  XXX needs more work, see the old malloc code.
818      */
819 #ifdef INVARIANTS
820     if (z->z_ChunkSize < sizeof(weirdary))
821 	bcopy(weirdary, chunk, z->z_ChunkSize);
822     else
823 	bcopy(weirdary, chunk, sizeof(weirdary));
824 #endif
825 
826     /*
827      * Add this free non-zero'd chunk to a linked list for reuse, adjust
828      * z_FirstFreePg.
829      */
830 #ifdef INVARIANTS
831     if ((uintptr_t)chunk < VM_MIN_KERNEL_ADDRESS)
832 	panic("BADFREE %p", chunk);
833 #endif
834     chunk->c_Next = z->z_PageAry[pgno];
835     z->z_PageAry[pgno] = chunk;
836 #ifdef INVARIANTS
837     if (chunk->c_Next && (uintptr_t)chunk->c_Next < VM_MIN_KERNEL_ADDRESS)
838 	panic("BADFREE2");
839 #endif
840     if (z->z_FirstFreePg > pgno)
841 	z->z_FirstFreePg = pgno;
842 
843     /*
844      * Bump the number of free chunks.  If it becomes non-zero the zone
845      * must be added back onto the appropriate list.
846      */
847     if (z->z_NFree++ == 0) {
848 	z->z_Next = slgd->ZoneAry[z->z_ZoneIndex];
849 	slgd->ZoneAry[z->z_ZoneIndex] = z;
850     }
851 
852     --type->ks_inuse[z->z_Cpu];
853     type->ks_memuse[z->z_Cpu] -= z->z_ChunkSize;
854 
855     /*
856      * If the zone becomes totally free, and there are other zones we
857      * can allocate from, move this zone to the FreeZones list.  Since
858      * this code can be called from an IPI callback, do *NOT* try to mess
859      * with kernel_map here.  Hysteresis will be performed at malloc() time.
860      */
861     if (z->z_NFree == z->z_NMax &&
862 	(z->z_Next || slgd->ZoneAry[z->z_ZoneIndex] != z)
863     ) {
864 	SLZone **pz;
865 
866 	for (pz = &slgd->ZoneAry[z->z_ZoneIndex]; z != *pz; pz = &(*pz)->z_Next)
867 	    ;
868 	*pz = z->z_Next;
869 	z->z_Magic = -1;
870 	z->z_Next = slgd->FreeZones;
871 	slgd->FreeZones = z;
872 	++slgd->NFreeZones;
873     }
874     crit_exit();
875 }
876 
877 /*
878  * kmem_slab_alloc()	(MP SAFE) (GETS BGL)
879  *
880  *	Directly allocate and wire kernel memory in PAGE_SIZE chunks with the
881  *	specified alignment.  M_* flags are expected in the flags field.
882  *
883  *	Alignment must be a multiple of PAGE_SIZE.
884  *
885  *	NOTE! XXX For the moment we use vm_map_entry_reserve/release(),
886  *	but when we move zalloc() over to use this function as its backend
887  *	we will have to switch to kreserve/krelease and call reserve(0)
888  *	after the new space is made available.
889  *
890  *	Interrupt code which has preempted other code is not allowed to
891  *	use PQ_CACHE pages.  However, if an interrupt thread is run
892  *	non-preemptively or blocks and then runs non-preemptively, then
893  *	it is free to use PQ_CACHE pages.
894  *
895  *	This routine will currently obtain the BGL.
896  */
897 static void *
898 kmem_slab_alloc(vm_size_t size, vm_offset_t align, int flags)
899 {
900     vm_size_t i;
901     vm_offset_t addr;
902     vm_offset_t offset;
903     int count, vmflags, base_vmflags;
904     thread_t td;
905     vm_map_t map = kernel_map;
906 
907     size = round_page(size);
908     addr = vm_map_min(map);
909 
910     /*
911      * Reserve properly aligned space from kernel_map.  RNOWAIT allocations
912      * cannot block.
913      */
914     if (flags & M_RNOWAIT) {
915 	if (try_mplock() == 0)
916 	    return(NULL);
917     } else {
918 	get_mplock();
919     }
920     count = vm_map_entry_reserve(MAP_RESERVE_COUNT);
921     crit_enter();
922     vm_map_lock(map);
923     if (vm_map_findspace(map, vm_map_min(map), size, align, &addr)) {
924 	vm_map_unlock(map);
925 	if ((flags & M_NULLOK) == 0)
926 	    panic("kmem_slab_alloc(): kernel_map ran out of space!");
927 	crit_exit();
928 	vm_map_entry_release(count);
929 	rel_mplock();
930 	return(NULL);
931     }
932     offset = addr - VM_MIN_KERNEL_ADDRESS;
933     vm_object_reference(kernel_object);
934     vm_map_insert(map, &count,
935 		    kernel_object, offset, addr, addr + size,
936 		    VM_PROT_ALL, VM_PROT_ALL, 0);
937 
938     td = curthread;
939 
940     base_vmflags = 0;
941     if (flags & M_ZERO)
942         base_vmflags |= VM_ALLOC_ZERO;
943     if (flags & M_USE_RESERVE)
944 	base_vmflags |= VM_ALLOC_SYSTEM;
945     if (flags & M_USE_INTERRUPT_RESERVE)
946         base_vmflags |= VM_ALLOC_INTERRUPT;
947     if ((flags & (M_RNOWAIT|M_WAITOK)) == 0)
948     	panic("kmem_slab_alloc: bad flags %08x (%p)", flags, ((int **)&size)[-1]);
949 
950 
951     /*
952      * Allocate the pages.  Do not mess with the PG_ZERO flag yet.
953      */
954     for (i = 0; i < size; i += PAGE_SIZE) {
955 	vm_page_t m;
956 	vm_pindex_t idx = OFF_TO_IDX(offset + i);
957 
958 	/*
959 	 * VM_ALLOC_NORMAL can only be set if we are not preempting.
960 	 *
961 	 * VM_ALLOC_SYSTEM is automatically set if we are preempting and
962 	 * M_WAITOK was specified as an alternative (i.e. M_USE_RESERVE is
963 	 * implied in this case), though I'm sure if we really need to do
964 	 * that.
965 	 */
966 	vmflags = base_vmflags;
967 	if (flags & M_WAITOK) {
968 	    if (td->td_preempted)
969 		vmflags |= VM_ALLOC_SYSTEM;
970 	    else
971 		vmflags |= VM_ALLOC_NORMAL;
972 	}
973 
974 	m = vm_page_alloc(kernel_object, idx, vmflags);
975 
976 	/*
977 	 * If the allocation failed we either return NULL or we retry.
978 	 *
979 	 * If M_WAITOK is specified we wait for more memory and retry.
980 	 * If M_WAITOK is specified from a preemption we yield instead of
981 	 * wait.  Livelock will not occur because the interrupt thread
982 	 * will not be preempting anyone the second time around after the
983 	 * yield.
984 	 */
985 	if (m == NULL) {
986 	    if (flags & M_WAITOK) {
987 		if (td->td_preempted) {
988 		    vm_map_unlock(map);
989 		    lwkt_yield();
990 		    vm_map_lock(map);
991 		} else {
992 		    vm_map_unlock(map);
993 		    vm_wait();
994 		    vm_map_lock(map);
995 		}
996 		i -= PAGE_SIZE;	/* retry */
997 		continue;
998 	    }
999 
1000 	    /*
1001 	     * We were unable to recover, cleanup and return NULL
1002 	     */
1003 	    while (i != 0) {
1004 		i -= PAGE_SIZE;
1005 		m = vm_page_lookup(kernel_object, OFF_TO_IDX(offset + i));
1006 		vm_page_free(m);
1007 	    }
1008 	    vm_map_delete(map, addr, addr + size, &count);
1009 	    vm_map_unlock(map);
1010 	    crit_exit();
1011 	    vm_map_entry_release(count);
1012 	    rel_mplock();
1013 	    return(NULL);
1014 	}
1015     }
1016 
1017     /*
1018      * Success!
1019      *
1020      * Mark the map entry as non-pageable using a routine that allows us to
1021      * populate the underlying pages.
1022      */
1023     vm_map_set_wired_quick(map, addr, size, &count);
1024     crit_exit();
1025 
1026     /*
1027      * Enter the pages into the pmap and deal with PG_ZERO and M_ZERO.
1028      */
1029     for (i = 0; i < size; i += PAGE_SIZE) {
1030 	vm_page_t m;
1031 
1032 	m = vm_page_lookup(kernel_object, OFF_TO_IDX(offset + i));
1033 	m->valid = VM_PAGE_BITS_ALL;
1034 	vm_page_wire(m);
1035 	vm_page_wakeup(m);
1036 	pmap_enter(kernel_pmap, addr + i, m, VM_PROT_ALL, 1);
1037 	if ((m->flags & PG_ZERO) == 0 && (flags & M_ZERO))
1038 	    bzero((char *)addr + i, PAGE_SIZE);
1039 	vm_page_flag_clear(m, PG_ZERO);
1040 	vm_page_flag_set(m, PG_MAPPED | PG_WRITEABLE | PG_REFERENCED);
1041     }
1042     vm_map_unlock(map);
1043     vm_map_entry_release(count);
1044     rel_mplock();
1045     return((void *)addr);
1046 }
1047 
1048 /*
1049  * kmem_slab_free()	(MP SAFE) (GETS BGL)
1050  */
1051 static void
1052 kmem_slab_free(void *ptr, vm_size_t size)
1053 {
1054     get_mplock();
1055     crit_enter();
1056     vm_map_remove(kernel_map, (vm_offset_t)ptr, (vm_offset_t)ptr + size);
1057     crit_exit();
1058     rel_mplock();
1059 }
1060 
1061